当前位置:网站首页>qt绘画事件-设置背景图片
qt绘画事件-设置背景图片
2022-07-28 05:25:00 【小土同学!】
前言
设置widget的背景图片,并在背景图片上放置别的图片
直接上代码:
头文件
void paintEvent(QPaintEvent *);
cpp文件:
void MainWindow::paintEvent(QPaintEvent *)
{
//创建画家,指定绘图设备
QPainter painter(this);
//创建QPixmap对象
QPixmap pix;
//加载图片
pix.load(":/res/PlayLevelSceneBg.png");
//绘制背景图
painter.drawPixmap(0,0,this->width(),this->height(),pix);
//加载标题
pix.load(":/res/Title.png");
//缩放图片
pix = pix.scaled(pix.width()*0.5,pix.height()*0.5);
//绘制标题
painter.drawPixmap( 10,30,pix.width(),pix.height(),pix);
}
paintEvent方法是qt原本就有的方法,调用绘画事件,只需重写该方法,在cpp文件中也无需主动调用

翻译:
处理在事件参数中传递的绘制事件。
默认实现什么也不做。 重新实现这个函数来执行绘画。 如有必要,可从事件中检索脏区。
效果图:

注意
图片的路径是我们需要注意的问题,因为qt采用影子构建的方法,可执行程序的位置和源文件不在同一个位置。图片的相对路径是根据可执行文件而定的,…/是可执行程序的当前路径
在右键图片,可快速复制图片路径

不会添加资源文件的同学,可查看这篇文章
链接:qt添加资源文件
码字不易,如果这篇博客对你有帮助,麻烦点赞收藏,非常感谢!有不对的地方,可以评论区交流。
边栏推荐
- Pytorch learning notes 3 - datasets & dataloaders & transforms
- EfficientNET_V1
- Bag of Tricks训练卷积网络的技巧
- Trouble encountered in cable testing -- a case study of a manufacturer?
- clickhouse聚合之内存不足怎么办?那就提升聚合性能
- Learning notes on hardware circuit design 2 -- step-down power circuit
- USB network native driver for esxi updated to support esxi7.0.1
- 当mysql表从压缩表变成普通表会发生什么
- USB network native driver for esxi updated to support esxi7.0 Update 2
- Matlab simulation of radar imaging 1 - LFM signal and its spectrum
猜你喜欢

ConNeXt

ASP. Net read database bound to treeview recursive mode

【YOLOv5】环境搭建:Win11 + mx450

Fluke dtx-sfm2 single mode module of a company in Hangzhou - repair case

PLC的选型

低功耗设计-Power Switch

A NOVEL DEEP PARALLEL TIME-SERIES RELATION NETWORK FOR FAULT DIAGNOSIS

Analysis of MOSFET damage at the moment of power failure of isolated power supply

Beta分布(概率的概率)

ICC2(三)Clock Tree Synthesis
随机推荐
Arduino reads the analog voltage_ How mq2 gas / smoke sensor works and its interface with Arduino
当mysql表从压缩表变成普通表会发生什么
A NOVEL DEEP PARALLEL TIME-SERIES RELATION NETWORK FOR FAULT DIAGNOSIS
Esxi on ARM v1.2 (updated in November 2020)
How to view the transfer function of the module directly built by Simulink
Analysis of MOSFET damage at the moment of power failure of isolated power supply
The short jumper dsx-8000 test is normal, but the dsx-5000 test has no length display?
clickhouse聚合之探索聚合内部机制
How to calibrate dsx2-8000? Calibration process?
set_ clock_ groups
mysql删表不删库
Beginners choose sensors
Ship detection in SAR image based on yolov5
Communication between DSP and FPGA
Chinese display problem of calendarextender control
Matlab simulation of radar imaging 1 - LFM signal and its spectrum
AEM online product promotion conference - Cable certification tester
Pytorch learning notes
Weight decay
IMS-FACNN(Improved Multi-Scale Convolution Neural Network integrated with a Feature Attention Mecha