当前位置:网站首页>Qt设置背景图片方法
Qt设置背景图片方法
2022-07-29 05:18:00 【cloud_yq】
本文主要介绍三种Qt设置背景图片的方法:
1、QPalette
2、重写paintEvent
3、设置Qss(Qt style sheet)
1、QPalette
(1)示例
//.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主要通过void QPalette::setBrush ( ColorRole role, const QBrush & brush );函数来设置背景图片,其中ColorRole设置为QPalette::window(具体信息查看QPalette的讲解)。当设置具有父窗体的窗体背景时,需要加上setAutoFillBackground(true)来显示背景。
(2)结果图
2、重写paintEvent
所有QWidget的子类都继承了QWidget中的虚函数:[virtual protected] void QWidget::paintEvent(QPaintEvent *event)。在新的窗体中通过对改虚函数进行重写,即可完成对窗体的背景进行设置。
(1)示例
//.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);
}
在本例中,重写paintEvent事件时使用QPainter类的 void QPainter::drawPixmap(int x, int y, int w, int h, const QPixmap &pixmap, int sx, int sy, int sw, int sh)方法对窗体的背景进行设置。
(2)结果图
3、设置Qss
Qss使用Qt开发的一种类似于Css的语法机制,能够设置各种样式的窗体。窗体通过使用void setStyleSheet(const QString &sheet)来设置与 字符串sheet相对应的样式。具体的Qss语法机制可查看帮助文档(Qt style sheet)。
(1)示例
//.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)结果图
4、三种方法简单比较
使用三种方法都可以设置背景图片,但是当窗体大小变化时,背景图片的覆盖方式会随着变化。
(1)QPalette设置的背景图片会重叠覆盖,如:
(2)paintEvent设置的图片会进行拉伸覆盖窗体,如:
(3)而设置Qss方式上面两种覆盖方式都可实现。
当Qss设置为 background-image:url(:/img.jpg) 则完成重叠式覆盖
当Qss设置为 border-image:url(:/img.jpg) 则完成拉伸式覆盖
本文是自己的学习笔记,可能不完全正确,仅供参考!
参考连接:1、https://wenku.baidu.com/view/f5cdfdeaaeaad1f346933f27
2、https://blog.csdn.net/qq_25800311/article/details/80874757
边栏推荐
- [typescript] type reduction (including type protection) and type predicate in typescript
- ClickHouse学习(九)clickhouse整合mysql
- HCIA-R&S自用笔记(24)ACL
- Integer overflow and printing
- [typescript] learn typescript object types in depth
- Abstract classes and interfaces
- Database operation day 6
- Detailed explanation of serial port communication
- JS deep copy - Notes
- Summary of knowledge points related to forms and forms
猜你喜欢
ClickHouse学习(七)表查询优化
B - 识别浮点常量问题
Wechat applet - component parameter transmission, state management
Detailed explanation of GPIO input and output
第三课threejs全景预览房间案例
ClickHouse学习(八)物化视图
Introduction to C language array to proficiency (array elaboration)
Question swiping Madness - leetcode's sword finger offer58 - ii Detailed explanation of left rotation string
365 day challenge leetcode 1000 questions - day 035 one question per day + two point search 13
ClickHouse学习(三)表引擎
随机推荐
Do students in the science class really understand the future career planning?
Longest string without duplicate characters
用threejs 技术做游戏跑酷
How does the MD editor of CSDN input superscripts and subscripts? The input method of formula and non formula is different
Relationship between redrawing and reflow
全局components组件注册
微信小程序更改属性值-setData-双向绑定-model
Clickhouse learning (IV) SQL operation
Alibaba cloud and Dingjie software released the cloud digital factory solution to realize the localized deployment of cloud MES system
Topological ordering of a graph of water
第三课threejs全景预览房间案例
无重复字符的最长字串
Side effects and sequence points
Allocate memory: malloc() and free()
Li Kou 994: rotten orange (BFS)
Liang Yuqi, founder of aitalk: the link between image and virtual reality
Using POI TL to insert multiple pictures and the same data of multiple rows of cells into the table cells of word template at one time, it is a functional component for automatic merging
Similarities and differences between REM and PX and EM
Niuke network programming problem - [wy22 Fibonacci series] and [replace spaces] detailed explanation
【TypeScript】深入学习TypeScript函数