当前位置:网站首页>QT painting event - set background picture
QT painting event - set background picture
2022-07-28 06:33:00 【Schoolmate Xiaotu!】
Preface
Set up widget Background image of , And put other pictures on the background picture
Go straight to the code :
The header file
void paintEvent(QPaintEvent *);
cpp file :
void MainWindow::paintEvent(QPaintEvent *)
{
// Creating painters , Specify the drawing device
QPainter painter(this);
// establish QPixmap object
QPixmap pix;
// Loading pictures
pix.load(":/res/PlayLevelSceneBg.png");
// Draw the background
painter.drawPixmap(0,0,this->width(),this->height(),pix);
// Load title
pix.load(":/res/Title.png");
// Zoom in and out
pix = pix.scaled(pix.width()*0.5,pix.height()*0.5);
// Draw the title
painter.drawPixmap( 10,30,pix.width(),pix.height(),pix);
}
paintEvent The method is qt The original method , Call painting event , Just rewrite the method , stay cpp There is no need to actively call in the file

translate :
Handle the drawing event passed in the event parameter .
The default implementation does nothing . Re implement this function to perform painting . If necessary, , Dirty areas can be retrieved from events .
design sketch :

Be careful
The path of pictures is a problem we need to pay attention to , because qt Adopt the method of shadow construction , The location of the executable program is not in the same location as the source file . The relative path of the image depends on the executable ,…/ Is the current path of the executable
Right click on the picture , Can quickly copy the image path

Students who will not add resource files , Check out this article
link :qt Add resource file
It's not easy to code words , If this blog is helpful to you , Please praise the collection , Thank you very much ! There is something wrong , Can comment on the area of communication .
边栏推荐
- 正反斜杠笔记
- Measure computer battery capacity
- Vs code basic configuration and beautification
- I heard that you are also practicing when I interviewed several junior interns.
- Why should fluke dsx2-5000 network cable tester be calibrated once a year?
- 雷达成像 Matlab 仿真 1 —— LFM信号及其频谱
- Efficient Net_ V2
- qt解析字符串转为json数据并解析
- mixup_ ratio
- 听说你也在实习|当我采访了几个大三实习生之后。
猜你喜欢
随机推荐
Systemmediasize startup option added in esxi 7.0 update 1C
【学习笔记】进程创建
【学习笔记】vim 编辑器
Word自动目录字体修改和行间距的问题
自定义组件--数据监听器
mysql join技巧
Transformer self attention mechanism and complete code implementation
Listener
qt设置加载界面的几种方法
详解安装msdn 2015及其注意事项
正反斜杠笔记
NPM yarn related operations
USB network native driver for esxi updated to support esxi7.0.1
Briefly introduce EMD decomposition, Hilbert transform and spectral method
PyTorch 学习笔记 4 —— 自动计算梯度下降 AUTOGRAD
【学习笔记】编码能力
MFC 使用控制台打印程序信息
毕业论文 | 文献综述应该怎么写
小程序自定义组件-数据,方法和属性
Perl入门学习(八)子程序









