当前位置:网站首页>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
边栏推荐
- ClickHouse学习(五)集群操作
- 【TypeScript】TypeScript中类型缩小(含类型保护)与类型谓词
- Relationship between link and @import
- With cloud simulation platform, Shichuang technology supports the upgrading of "China smart manufacturing"
- Hcia-r & s self use notes (27) comprehensive experiment
- Solve the problem that the prompt information of form verification does not disappear and the assignment does not take effect
- [C language series] - storage of deep anatomical data in memory (I) opening of summer vacation
- table中同一列中合并相同项
- Introduction to C language array to proficiency (array elaboration)
- 常见特征工程操作
猜你喜欢

用threejs 技术做游戏跑酷

VIM editor use

Solve the problem that the prompt information of form verification does not disappear and the assignment does not take effect

Day 1

Day 5

Clickhouse learning (V) cluster operation

shell基本操作(上)

Set the background color of a cell in the table

365 day challenge leetcode 1000 questions - day 035 one question per day + two point search 13

ClickHouse学习(十一)clickhouseAPI操作
随机推荐
uniapp组件之选择选项(如套餐选择)
Three handshakes and four waves for the interview summary
C language first level pointer
Day 3
[C language series] - print prime numbers between 100 and 200
[untitled]
Hcia-r & s self use notes (26) PPP
表格与表单相关知识点总结
关于局部变量
【TypeScript】深入学习TypeScript对象类型
Detailed explanation of exit interrupt
HCIA-R&S自用笔记(26)PPP
微信小程序视频上传组件直接上传至阿里云OSS
[C language series] - storage of deep anatomical data in memory (II) - floating point type
无重复字符的最长字串
How does the MD editor of CSDN input superscripts and subscripts? The input method of formula and non formula is different
ANSI C type qualifier
rem与px与em异同点
link与@import导入外部样式的区别
[C language series] - realize the exchange of two numbers without creating the third variable