当前位置:网站首页>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
边栏推荐
- uniapp页面标题显示效果
- ·Let's introduce ourselves to the way of programming·
- JS deep copy - Notes
- table中同一列中合并相同项
- Three handshakes and four waves for the interview summary
- Merge the same items in the same column in table
- One dimensional array exercise
- [JS question solution] questions 1-10 in JS of niuke.com
- Longest string without duplicate characters
- 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
猜你喜欢

Detailed installation and use tutorial of MySQL (nanny installation with pictures and texts)

Global components component registration

PyQt5:第一章第1节:使用Qt组件创建一个用户界面-介绍

eggjs 创建应用知识点

Clickhouse learning (IX) Clickhouse integrating MySQL

Playwright实战案例之爬取js加密数据

table中同一列中合并相同项

ClickHouse学习(二)ClickHouse单机安装

Pyqt5: Chapter 1, Section 1: creating a user interface using QT components - Introduction

uniapp之常用提示弹框
随机推荐
[JS question solution] questions 1-10 in JS of niuke.com
uniapp之常用提示弹框
C language first level pointer
[C language series] - detailed explanation of file operation (Part 1)
Cmu15-213 shell lab experiment record
[sword finger offer] - explain the library function ATOI and simulate the realization of ATOI function
HCIA-R&S自用笔记(27)综合实验
Clickhouse learning (XI) clickhouseapi operation
关于局部变量
Clickhouse learning (IV) SQL operation
[C language series] - storage of deep anatomical data in memory (I) opening of summer vacation
Hcia-r & s self use notes (26) PPP
Three handshakes and four waves for the interview summary
ClickHouse学习(十)监控运行指标
Basic concepts of MySQL + database system structure + extended application + basic command learning
Do students in the science class really understand the future career planning?
Integer overflow and printing
实现table某个单元格背景色设置
ClickHouse学习(五)集群操作
Hcia-r & s self use notes (25) NAT technical background, NAT type and configuration