当前位置:网站首页>How QT sets some areas to be transparent in the background image
How QT sets some areas to be transparent in the background image
2022-06-27 14:23:00 【hellokandy】
Client development , You may encounter the need to use some “ Irregular figure ” To fill the background . Or you need to set a certain area of the background to “ Hollowing out ”, That's transparency ! Basically follow these two steps , You can achieve the effect you want :
1、 Remove the title bar 、 Set the window property to transparent
setWindowFlags(windowFlags() | Qt::FramelessWindowHint ); // no border
setAttribute(Qt::WA_TranslucentBackground);
2、 rewrite paintEvent(QPaintEvent* event)
void paintHollowRect::paintEvent(QPaintEvent* event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::HighQualityAntialiasing);
// Make an area of the mask transparent
#if 0
// Draw a shadow mask
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);
// The most common type is SourceOver( Often referred to as alpha blend )
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);
// Draw background picture
painter.drawImage(this->rect(), QImage(img));
//
painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
// Overlay transparent rectangular areas , To display the image
painter.fillRect(QRect(10, 10, 300, 300), QColor(0, 0, 0, 0));
QWidget::paintEvent(event);
}
3、 design sketch


Be careful : As long as the dialog box has a border , There is no way to achieve transparency , So the border must be removed , And set the attribute to transparent , To get through paintEvent To fulfill your purpose .
边栏推荐
- AutoCAD - line width setting
- 现在开户有优惠吗?网上开户是否安全么?
- Abnormal analysis of pcf8591 voltage measurement data
- Use GCC to generate an abstract syntax tree "ast" and dump it to Dot file and visualization
- 国产数据库乱象
- 【mysql进阶】MTS主从同步原理及实操指南(七)
- 简析国内外电商的区别
- Pisa-Proxy 之 SQL 解析实践
- 招标公告:暨南大学附属第一医院Oracle数据库维保服务采购
- [PHP code injection] common injectable functions of PHP language and utilization examples of PHP code injection vulnerabilities
猜你喜欢

AutoCAD - line width setting

Use GCC to generate an abstract syntax tree "ast" and dump it to Dot file and visualization

巧用redis实现点赞功能,它不比mysql香吗?

基于 Nebula Graph 构建百亿关系知识图谱实践

LVI: feature extraction and sorting of lidar subsystem

Redis master-slave replication, sentinel mode, cluster cluster

隱私計算FATE-離線預測

CCID Consulting released the database Market Research Report on key application fields during the "14th five year plan" (attached with download)

赛迪顾问发布《“十四五” 关键应用领域之数据库市场研究报告》(附下载)
![[business security 03] password retrieval business security and interface parameter account modification examples (based on the metinfov4.0 platform)](/img/29/73c381f14a09ecaf36a98d67d76720.png)
[business security 03] password retrieval business security and interface parameter account modification examples (based on the metinfov4.0 platform)
随机推荐
How to set the compatibility mode of 360 speed browser
Make a ThreadLocal (source code) that everyone can understand
剑指 Offer II 039. 直方图最大矩形面积 单调栈
CV领域一代宗师黄煦涛教授86岁冥诞,UIUC专设博士奖学金激励新锐
OpenSSF安全计划:SBOM将驱动软件供应链安全
A brief analysis of the differences between domestic and foreign e-commerce
[WUSTCTF2020]girlfriend
[PHP code injection] common injectable functions of PHP language and utilization examples of PHP code injection vulnerabilities
招标公告:上海市研发公共服务平台管理中心Oracle一体机软硬件维保项目
[business security-04] universal user name and universal password experiment
为什么 Oracle 云客户必须在Oracle Cloud 季度更新发布后自行测试?
Completely solve the problem of Chinese garbled code in Web Engineering at one time
How ASP connects Excel
Great God developed the new H5 version of arXiv, saying goodbye to formula typography errors in one step, and the mobile phone can easily read literature
AutoCAD - line width setting
Getting to know cloud native security for the first time: the best guarantee in the cloud Era
招标公告:暨南大学附属第一医院Oracle数据库维保服务采购
AXI總線
Pycharm安装与设置
优雅的自定义 ThreadPoolExecutor 线程池