当前位置:网站首页>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

边栏推荐
- Modular notes
- 1190. invert the substring between each pair of parentheses
- The gap between the parent box and the child box
- CRMEB/V4.4标准版打通版商城源码小程序公众号H5+App商城源码
- Notes on learning Es5 and ES6
- Senior openstacker - Bloomberg, vexxhost upgraded to gold member of openinfra Foundation
- Mybags puls will report an error invalid bound statement (not found) when writing an SQL statement in the XML file:
- C language inherits memory management mechanism (unfinished)
- 自动化测试的生命周期是什么?
- Use definite integral to calculate triangle area
猜你喜欢

Education expert wangzhongze shared his experience for many years: family education is not a vassal

big. Js-- use / instance

2022 melting welding and thermal cutting exam exercises and answers

Niuke wrong question 3.1
![[analysis of STL source code] summary notes (3): vector introduction](/img/70/faa40c273f6b3a6b124fb870058489.jpg)
[analysis of STL source code] summary notes (3): vector introduction

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

Multi thread review summary parsing volatile keyword

Outer margin collapse
![[compilation principle] 05- syntax guided semantic computing -- Semantic Computing Based on translation mode](/img/7d/f3ba5a69e182160a5e1b51c7d9aaf9.png)
[compilation principle] 05- syntax guided semantic computing -- Semantic Computing Based on translation mode

Education expert Mr. wangzhongze: family education focuses on self growth
随机推荐
JVM Learning record (7) - - class Loading Process and parent delegation Model
[Oracle database] mammy tutorial day04 Sorting Query
【CF#262 (Div. 2)】 A. Vasya and Socks
Mobile console Gobang (first draft of detailed design)
Smart pointer (simple version)
软件测试周刊(第75期):唯有平视,才能看见真实的自己。
Atom, the top stream editor, will leave the historical stage on December 15
P3811 [template] multiplicative inverse
1、 Sqlserver2008 installation (with password), database creation, C form project test
Software testing weekly (issue 75): only when you look down, can you see your true self.
Interview question 17.08 Circus tower
Analyse du contrat du modèle de taux composé
一、SQLServer2008安裝(帶密碼)、創建數據庫、C#窗體項目測試
QT table display data
【LeetCode】-- 17. Letter combination of telephone number
C+tinycthread implementation thread
Gobang interface of mobile console (C language)
2022 low voltage electrician test questions and online simulation test
Create a form whose client area is 800 pixels by 600 pixels
JVM学习记录(七)——类加载过程与双亲委派模型