当前位置:网站首页>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添加资源文件
码字不易,如果这篇博客对你有帮助,麻烦点赞收藏,非常感谢!有不对的地方,可以评论区交流。
边栏推荐
- 低功耗设计-isolation cell
- How to calibrate dsx2-8000? Calibration process?
- Pycharm2019设置编辑器主题和默认代码
- How to test industrial Ethernet cables (using fluke dsx-8000)?
- ConNeXt
- Perl入门学习(九)引用
- PyTorch 学习笔记 1 —— Quick Start
- Best practices to ensure successful deployment of Poe devices
- Weight decay
- Matlab simulation of radar imaging 2 - pulse compression and windowing
猜你喜欢

Learning notes on hardware circuit design 2 -- step-down power circuit

Bert bidirectional encoder based on transformer

set_multicycle_path

clickhouse建宽表多少列最合适?

Communication between DSP and FPGA

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

雷达成像 Matlab 仿真 3 —— 多目标检测

Synopsys Multivoltage Flow

When to replace jack socket for dsx-pc6 jumper module?

Detailed explanation of word mail merge function: after merging, multiple word documents are generated and blank pages are deleted
随机推荐
Varistor design parameters and classic circuit recording hardware learning notes 5
ConNeXt
RS232 RS485 RS422 communication learning and notes
EMC experiment practical case ESD electrostatic experiment
MATLAB signal processing
When to replace jack socket for dsx-pc6 jumper module?
Shuffle Net_ v1-shuffle_ v2
What is the AEM testpro cv100 and fluke dsx-8000 of category 8 network cable tester?
Electric fast burst (EFT) design - EMC series hardware design notes 4
Perl入门学习(九)引用
Beginners choose sensors
EfficientNET_ V1
Graduation thesis | how to write literature review
AEM online product promotion conference - Cable certification tester
Convert data in grilview into datatable
What about the insufficient memory of Clickhouse aggregation? Then improve the polymerization performance
How to view the transfer function of the module directly built by Simulink
Pytorch learning note 4 - automatic calculation of gradient descent autograd
Pytorch learning notes 2 - about tensor
雷达成像 Matlab 仿真 2 —— 脉冲压缩与加窗