当前位置:网站首页>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来实现你的意图。
边栏推荐
- AXI總線
- Quickly set up a website to visit foreign countries, set up SS and start BBR to quickly surf the Internet
- 【PHP代码注入】PHP语言常见可注入函数以及PHP代码注入漏洞的利用实例
- 简析国内外电商的区别
- Multithreading Basics (III)
- [XMAN2018排位赛]通行证
- Make a ThreadLocal (source code) that everyone can understand
- Pytorch learning 3 (test training model)
- Kyndryl partnered with Oracle and Veritas
- [xman2018 qualifying] pass
猜你喜欢

Naacl 2022 | TAMT: search the transportable Bert subnet through downstream task independent mask training

Debug tool

反射学习总结

Acwing game 57

对半查找(折半查找)

隐私计算FATE-离线预测

Julia1.1 installation instructions

Half find (half find)

Tsinghua & Shangtang & Shanghai AI & CUHK proposed Siamese image modeling, which has both linear probing and intensive prediction performance

AcWing 第57 场周赛
随机推荐
Type 'image' is not a subtype of type 'imageprovider < object > solution
【业务安全-04】万能用户名及万能密码实验
Privacy computing fat offline prediction
What if the win system cannot complete the update and is revoking the status change
PCL Library - error reporting solution: cmake and Anaconda conflicts during installation
反射学习总结
Debug tool
注解学习总结
类模板中可变参的逐步展开
enable_if
In the past, domestic mobile phones were arrogant in pricing and threatened that consumers would like to buy or not, but now they have plummeted by 2000 for sale
Massive data! Second level analysis! Flink+doris build a real-time data warehouse scheme
EventLoop learning
American chips are hit hard again, and another chip enterprise after Intel will be overtaken by Chinese chips
AXI總線
国产数据库乱象
Buuctf Misc
Pytoch learning 2 (CNN)
【业务安全-01】业务安全概述及测试流程
基于SSM的Web网页聊天室系统