当前位置:网站首页>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添加资源文件
码字不易,如果这篇博客对你有帮助,麻烦点赞收藏,非常感谢!有不对的地方,可以评论区交流。
边栏推荐
- ICC2(三)Clock Tree Synthesis
- Pycharm2019 set editor theme and default code
- PLC的选型
- Bag of Tricks训练卷积网络的技巧
- VAN(DWConv+DWDilationConv+PWConv)
- 低功耗设计-isolation cell
- What about the insufficient memory of Clickhouse aggregation? Then improve the polymerization performance
- set_ clock_ groups
- Distinguishing PCB quality by color is a joke in itself
- Install visual studio 2019 steps and vs2019 offline installation package on win7
猜你喜欢

Shuffle Net_v1-shuffle_v2

The short jumper dsx-8000 test is normal, but the dsx-5000 test has no length display?

set_false_path

IMS-FACNN(Improved Multi-Scale Convolution Neural Network integrated with a Feature Attention Mecha

雷达成像 Matlab 仿真 1 —— LFM信号及其频谱

Detailed explanation of creepage distance and electrical clearance

Ship detection in SAR image based on yolov5

PLC的整体认识

How to calibrate dsx2-8000? Calibration process?

Random life-1
随机推荐
How to view the transfer function of the module directly built by Simulink
Synopsys Multivoltage Flow
mixup_ ratio
Cronbach’s α? Kmo coefficient? Factor load? The most understandable course of questionnaire reliability and validity analysis in history!!! (SPSS and AMOS)
Distinguishing PCB quality by color is a joke in itself
set_ clock_ groups
Matlab simulation of radar imaging 1 - LFM signal and its spectrum
RS232 RS485 RS422 communication learning and notes
ConNeXt
Surge impact immunity experiment (surge) -emc series Hardware Design Notes 6
How can fluke dsx2-5000 and dsx2-8000 modules find the calibration expiration date?
An example of bill printing
Electric fast burst (EFT) design - EMC series hardware design notes 4
Arduino reads the analog voltage_ How mq2 gas / smoke sensor works and its interface with Arduino
低功耗设计-Power Switch
VB OCX applied to Web
Perl入门学习(十一)文件操作
CString to char[] function
雷达成像 Matlab 仿真 3 —— 多目标检测
I heard that you are also practicing when I interviewed several junior interns.