当前位置:网站首页>QT 如何在背景图中将部分区域设置为透明
QT 如何在背景图中将部分区域设置为透明
2022-06-27 14:11:00 【hellokandy】
客户端开发时,可能会遇到需要使用一些“异形图”来填充背景。又或者是需要把背景某个区域设置为“镂空”,也就是透明!基本上按照以下两个步骤,就可以实现你想要的效果:
1、去掉标题栏、设置窗口属性为透明
setWindowFlags(windowFlags() | Qt::FramelessWindowHint ); // 无边框
setAttribute(Qt::WA_TranslucentBackground);
2、重写 paintEvent(QPaintEvent* event)
void paintHollowRect::paintEvent(QPaintEvent* event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::HighQualityAntialiasing);
//把遮罩的某个区域设置为透明
#if 0
//画阴影遮罩
painter.fillRect(0, 0, rect().width(), rect().height(), QColor(0, 0, 0, 88));
//
int w = rect().width() / 2;
int h = rect().height() / 2;
int l = rect().left() + (w / 2);
int t = rect().top() + (h / 2);
//最常见的类型是SourceOver(通常称为alpha混合)
painter.setCompositionMode(QPainter::CompositionMode_Source);
painter.fillRect(l, t, w, h, QColor(255, 255, 255, 10));
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
#endif
QString img = ":/res/bg_main.png";
QImageReader reader(img);
QSize size = reader.size();
resize(size);
//画背景图片
painter.drawImage(this->rect(), QImage(img));
//
painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
//叠加透明的矩形区域,以显示图像
painter.fillRect(QRect(10, 10, 300, 300), QColor(0, 0, 0, 0));
QWidget::paintEvent(event);
}
3、效果图


注意:只要对话框带有边框,就没办法实现透明,所以必须要去掉边框,以及把属性设置为透明,才可以通过paintEvent来实现你的意图。
边栏推荐
- How to use 200 lines of code to implement Scala's Object Converter
- A statistical problem of shell script
- Bidding announcement: Oracle all-in-one machine software and hardware maintenance project of Shanghai R & D Public Service Platform Management Center
- Julia1.1 installation instructions
- Daily question 3 (2): check binary string field
- [business security 03] password retrieval business security and interface parameter account modification examples (based on the metinfov4.0 platform)
- CCID Consulting released the database Market Research Report on key application fields during the "14th five year plan" (attached with download)
- EventLoop learning
- Acwing game 57
- Axi bus
猜你喜欢
随机推荐
【业务安全03】密码找回业务安全以及接口参数账号修改实例(基于metinfov4.0平台)
Interviewer: do you understand redis' shared object pool?
How to split microservices
Crane: a new way of dealing with dictionary items and associated data
[WUSTCTF2020]girlfriend
PostgreSQL 15新版本特性解读(含直播问答、PPT资料汇总)
The global chip market may stagnate, and China's chip expansion accelerates to improve its self-sufficiency rate against the trend
CMOS级电路分析
The second part of the travel notes of C (Part II) structural thinking: Zen is stable; all four advocate structure
Learning records of numpy Library
清华&商汤&上海AI&CUHK提出Siamese Image Modeling,兼具linear probing和密集预测性能!...
PCL Library - error reporting solution: cmake and Anaconda conflicts during installation
Why must Oracle cloud customers self test after the release of Oracle cloud quarterly update?
【每日3题(3)】盒子中小球的最大数量
Principle Comparison and analysis of mechanical hard disk and SSD solid state disk
How to use 200 lines of code to implement Scala's Object Converter
【PHP代码注入】PHP语言常见可注入函数以及PHP代码注入漏洞的利用实例
【业务安全-02】业务数据安全测试及商品订购数量篡改实例
R language objects are stored in JSON
Li Kou's 81st biweekly match





![[WUSTCTF2020]girlfriend](/img/a8/33fe5feb7bcbb73ba26a94d226cc4d.png)


