当前位置:网站首页>QT picture adaptive display control
QT picture adaptive display control
2022-06-11 07:28:00 【Fantastical Yu ruiyin】
Qt Picture adaptive display control
Write it at the front
Used to Qt Students who show pictures should find , Configuration is really uncomfortable , Use QLabel The display image is prone to incomplete display of the image or the deformation of the control caused by the large image , Quite uncomfortable . To realize the adaptive display of pictures, you generally need to use painterevent To configure the page , For some beginners, there may be a certain difficulty in configuring , After all, I am divorced from .ui You may not know how to design the page . So here I make a control that can adaptively display pictures , Only need to QWidget Control can be promoted to this control to realize the adaptive display of pictures .
Go straight to the code
The header file
#ifndef MYIMAGE_H
#define MYIMAGE_H
#include <QWidget>
#include <QPainter>
#include <QImage>
// Use painterevent, Let pictures follow widget The size of the self-adaptive adjustment
class MyImage:public QWidget
{
Q_OBJECT
public:
explicit MyImage(QWidget *parent = nullptr);
~MyImage();
void setImage(QImage image);
protected:
void paintEvent(QPaintEvent *);
private:
QImage image;
};
#endif // MYIMAGE_H
cpp file
#include "myimage.h"
MyImage::MyImage(QWidget *parent)
{
image.load(":/image.png");
}
MyImage::~MyImage()
{
}
void MyImage::paintEvent(QPaintEvent *)
{
int width=this->width();
int height=this->height();
QPainter painter(this);
painter.save();
// Resize the picture to the size of the form
image.scaled(width,height);
// Draw a picture
painter.drawImage(this->rect(),image);
painter.restore();
painter.setRenderHints(QPainter::Antialiasing|QPainter::TextAntialiasing);
// painter.translate(width/2,height/2);
// It can be set as a square , Smallest size bit 200*200;
int side =qMin(width,height);
painter.scale(side/200,side/200);
}
void MyImage::setImage(QImage image)
{
this->image=image;
update();
}
How to use
Add the above two files to the project ( No, .ui file ), Add a... In the display interface QWidget Control , And will QWidget Upgrade to MyImage, You only need to use setImage Function to refresh the image .
According to the effect

边栏推荐
- Pat class A by category
- QT interface nested movement based on qscrollarea
- Education expert Mr. wangzhongze: family education focuses on self growth
- Classification of MNIST datasets with keras
- Biological sequence intelligent analysis platform blog (1)
- Analyse du contrat du modèle de taux composé
- [STL source code analysis] summary notes (1): Opening
- 学 SQL 必须了解的10个高级概念
- Interview question 02.06 Palindrome linked list
- MS office level II wrong question record [8]
猜你喜欢

【编译原理】05-语法制导的语义计算——基于翻译模式的语义计算

QT 基于QScrollArea的界面嵌套移动

The rotation of the earth and the moon (II)

JVM学习记录(七)——类加载过程与双亲委派模型

CRMEB/V4.4标准版打通版商城源码小程序公众号H5+App商城源码

如果要存 IP 地址,用什么数据类型比较好?99%人都会答错!
![[STL source code analysis] summary notes (10): hashtable exploration](/img/31/a77ac380dbd0f85957bd1df1b906f5.jpg)
[STL source code analysis] summary notes (10): hashtable exploration

Senior openstacker - Bloomberg, vexxhost upgraded to gold member of openinfra Foundation

【Oracle 数据库】奶妈式教程day04 排序查询

Building a full-featured NAS server with raspberry pie (06): built-in file synchronization tool for penetration
随机推荐
Summary of classic interview questions
gaussDB for redis和 redis的区别?
Leetcode-104. Maximum Depth of Binary Tree
Crmeb/v4.4 Standard Version open version mall source code applet official account h5+app mall source code
【Oracle 数据库】奶妈式教程day02 数据库管理工具SQLPLUS的使用
C language to write a calculator calculation logic
Several transaction modes of Seata
C language to write a calculator MVC (very interesting code architecture callback and constructor mode and the use of pointer functions)
Mobile console Gobang (first draft of detailed design)
MySQL设置管理员密码无法生效的案例一则
Server parameter adjustment record
Smart pointer (simple version)
[analysis of STL source code] summary note (4): behind the scenes hero allocator
多线程复习总结之解析Volatile关键字
MS office level II wrong question record [10]
The rotation of the earth and the moon (II)
[STL source code analysis] summary note (2): overview of containers
[Oracle database] mammy tutorial day03 Sorting Query
If you want to save an IP address, what data type is better? 99% of people will answer wrong!
[STL source code analysis] summary notes (10): hashtable exploration