当前位置:网站首页>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 .
边栏推荐
- 赛迪顾问发布《“十四五” 关键应用领域之数据库市场研究报告》(附下载)
- PCL库——报错解决:安装时遇到的cmake与anaconda的冲突问题
- Privacy computing fat offline prediction
- Domestic database disorder
- 2022-06-27日报:Swin Transformer、ViT作者等共话:好的基础模型是CV研究者的朴素追求
- Design and implementation of reading app based on Web Platform
- 高德地图IP定位2.0备份
- PCL Library - error reporting solution: cmake and Anaconda conflicts during installation
- CV领域一代宗师黄煦涛教授86岁冥诞,UIUC专设博士奖学金激励新锐
- ERROR L104: MULTIPLE PUBLIC DEFINITIONS
猜你喜欢
![[business security-04] universal user name and universal password experiment](/img/09/73d8356d00cefb6d1af086669f69ff.png)
[business security-04] universal user name and universal password experiment

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

反射学习总结

Make a ThreadLocal (source code) that everyone can understand

Too many requests at once, and the database is in danger

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

enable_if

R language objects are stored in JSON

Semaphore of thread synchronization
Talk about redis transactions
随机推荐
简析国内外电商的区别
Web chat room system based on SSM
Li Kou's 81st biweekly match
my. INI file configuration
522. longest special sequence II / Sword finger offer II 101 Split equal sum subset
国产数据库乱象
What if the win system cannot complete the update and is revoking the status change
How to solve the problem of missing language bar in win10 system
Redis 主从复制、哨兵模式、Cluster集群
enable_ if
基于Vue+Node+MySQL的美食菜谱食材网站设计与实现
[business security 03] password retrieval business security and interface parameter account modification examples (based on the metinfov4.0 platform)
每日3题(1):找到最近的有相同 X 或 Y 坐标的点
SFINAE
机械硬盘和ssd固态硬盘的原理对比分析
Kyndryl partnered with Oracle and Veritas
Is there any discount for opening an account now? Is it safe to open an account online?
Rereading the classic: the craft of research (1)
2022-06-27日报:Swin Transformer、ViT作者等共话:好的基础模型是CV研究者的朴素追求
Library management system