当前位置:网站首页>QT setting background image method
QT setting background image method
2022-07-29 05:42:00 【cloud_ yq】
This article mainly introduces three kinds of Qt How to set the background picture :
1、QPalette
2、 rewrite paintEvent
3、 Set up Qss(Qt style sheet)
1、QPalette
(1) Example
//.cpp
this->resize(450,700);
QPalette pa(this->palette());
QImage img = QImage(":/img/01.jpg");
img = img.scaled(this->size());
QBrush *pic = new QBrush(img);
pa.setBrush(QPalette::Window,*pic);
//this->setAutoFillBackground(true);
this->setPalette(pa);QPalette Mainly through void QPalette::setBrush ( ColorRole role, const QBrush & brush ); Function to set the background image , among ColorRole Set to QPalette::window( See for details QPalette Explanation ). When setting the form background with the parent form , Need to add setAutoFillBackground(true) To display the background .
(2) Result chart

2、 rewrite paintEvent
all QWidget All subclasses of inherit QWidget Virtual function in :[virtual protected] void QWidget::paintEvent(QPaintEvent *event). Rewrite the virtual function in the new form , You can finish setting the background of the form .
(1) Example
//.cpp
#include "pain.h"
#include <QPainter>
#include <QPixmap>
#include "palette.h"
Pain::Pain(QWidget *parent) : QWidget(parent)
{
this->resize(450,700);
this->setWindowTitle("PainEvent");
}
void Pain::paintEvent(QPaintEvent *event)
{
QPainter *painter = new QPainter(this);
QPixmap pic = QPixmap(":/img/03.jpg");
//pic = pic.scaled(this->width(),this->height());
painter->drawPixmap(0,0,this->width(),this->height(),pic);
}
In this case , rewrite paintEvent Event using QPainter Class void QPainter::drawPixmap(int x, int y, int w, int h, const QPixmap &pixmap, int sx, int sy, int sw, int sh) Method to set the background of the form .
(2) Result chart

3、 Set up Qss
Qss Use Qt The developed one is similar to Css The grammatical mechanism of , Can set various styles of forms . Form by using void setStyleSheet(const QString &sheet) To set and character string sheet Corresponding style . Concrete Qss Syntax mechanism can view help documents (Qt style sheet).
(1) Example
//.cpp
#include "setstyle.h"
#include "pain.h"
#include <QFrame>
SetStyle::SetStyle(QFrame *parent) : QFrame(parent)
{
this->resize(450,700);
this->setWindowTitle("Qss");
this->setStyleSheet("QFrame{background-image:url(:/img/02.jpg)}");
}
(2) Result chart

4、 The three methods are simply compared
You can set the background image using all three methods , But when the size of the form changes , The coverage of the background image will change .
(1)QPalette The set background image will overlap , Such as :

(2)paintEvent The set picture will stretch and cover the form , Such as :

(3) And set up Qss The above two coverage methods can be realized .
When Qss Set to background-image:url(:/img.jpg) Then complete the overlapping coverage
When Qss Set to border-image:url(:/img.jpg) Complete stretch coverage
This is my study notes , It may not be completely correct , For reference only !
Refer to the connection :1、https://wenku.baidu.com/view/f5cdfdeaaeaad1f346933f27
2、https://blog.csdn.net/qq_25800311/article/details/80874757
边栏推荐
- Dynamic sorting of DOM object element blocks in applets
- rem与px与em异同点
- Flask 报错 RuntimeError: The session is unavailable because no secret key was set.
- 全局components组件注册
- ·Let's introduce ourselves to the way of programming·
- link与@import的关系
- ClickHouse学习(八)物化视图
- Detailed explanation of GPIO input and output
- Solve the problem that the prompt information of form verification does not disappear and the assignment does not take effect
- 微信小程序-屏幕高度
猜你喜欢

【JS题解】牛客网JS篇1-10题

B - identify floating point constant problems

HCIA-R&S自用笔记(26)PPP
![[C language series] - constants and variables that confuse students](/img/24/1158034a5de413ea4ce160c5bfcbb4.png)
[C language series] - constants and variables that confuse students

ClickHouse学习(十)监控运行指标

Qt布局管理--部件拉伸(Stretch)原理及大小策略(sizePolicy)

AR虚拟增强与现实

Global components component registration

Flask 报错 RuntimeError: The session is unavailable because no secret key was set.

Realize simple database query (incomplete)
随机推荐
Niuke network programming problem - [wy22 Fibonacci series] and [replace spaces] detailed explanation
AR虚拟增强与现实
[C language series] - constants and variables that confuse students
[typescript] in depth study of typescript functions
全局components组件注册
365 day challenge leetcode 1000 questions - day 035 one question per day + two point search 13
Liang Yuqi, founder of aitalk: the link between image and virtual reality
Clickhouse learning (VII) table query optimization
uniapp之常用提示弹框
Hcia-r & s self use notes (27) comprehensive experiment
2022 mathematical modeling competition summer training lecture - optimization method: goal planning
QFrame类学习笔记
HCIA-R&S自用笔记(27)综合实验
Playwright实战案例之爬取js加密数据
【JS题解】牛客网JS篇1-10题
End of document
弹性盒子flex
Qtcreator+cmake compiler settings
JS simple code determines whether the device that opens the page is the PC end of the computer, the H5 end of the mobile phone, or the wechat end
Day 5