当前位置:网站首页>QT实现圆角窗口
QT实现圆角窗口
2022-07-06 09:28:00 【Larry_Yanan】
用QT做界面开发的时候,时不时会有一些比较刁难人的样式要求,比如你需要将一个独立窗口,设置成圆角样式。
//UI文件中,对最外层窗口的样式设置
#ChooseUsbCamera{ //类名
background-color: rgb(255, 255, 255); //背景颜色(白色)
border-radius: 10px; //四角圆角的直径
border-style:solid; //边框样式(实线)
border-width:2px; //边框宽度
border-color:rgb(213, 213, 213); //边框颜色
}
这样,就简单做出一个圆角并且带有边框的界面效果,比较的美观。
然而那么问题来了,虽然设置成圆角之后,但四个顶角的透明化无法实现,这自然而然会在窗口初始化的时候加入透明化的代码:
setAttribute(Qt::WA_TranslucentBackground);
但是却变成窗口背景的完全透明化,即便你设置了背景相关的样式
这可能是因为透明化的设置,覆盖掉了我们自定义的样式。这个时候则需要重载paintEvent事件,重新刷图
void ChooseUsbCamera::paintEvent(QPaintEvent *event)
{
QStyleOption opt;
opt.init(this);
QPainter painter(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
}
于是,终于实现了这个简单但令人头疼的圆角窗口效果:
可以看到,它的四个角是透明的,实现了圆角的效果。而且还有一点点灰色的边框效果,虽然不是很明显hh。
其实如果你是窗口内的部件,用同样的方法设置样式,或者直接填充带有透明度的图片,是不需要这么麻烦的。但顶层的窗口设置起来往往就不行,研究了半天还走了不少弯路hh,所以特意记录一下。
边栏推荐
- 7-1 understand everything (20 points)
- Socket communication
- 【练习-7】Crossword Answers
- 渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
- 605. Planting flowers
- C basic grammar
- Quick to typescript Guide
- Radar equipment (greedy)
- [analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class
- 1689. Ten - the minimum number of binary numbers
猜你喜欢

【高老师UML软件建模基础】20级云班课习题答案合集

渗透测试 ( 1 ) --- 必备 工具、导航

b站 实时弹幕和历史弹幕 Protobuf 格式解析

Flask框架配置loguru日志庫

C language is the watershed between low-level and high-level

信息安全-威胁检测-flink广播流BroadcastState双流合并应用在过滤安全日志

渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus

Openwrt build Hello ipk

Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs

Penetration test (1) -- necessary tools, navigation
随机推荐
【练习-9】Zombie’s Treasure Chest
渗透测试 ( 1 ) --- 必备 工具、导航
【练习-10】 Unread Messages(未读消息)
Determine the Photo Position
Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
409. Longest palindrome
Penetration test (8) -- official document of burp Suite Pro
【练习-11】4 Values whose Sum is 0(和为0的4个值)
Analysis of protobuf format of real-time barrage and historical barrage at station B
1855. Maximum distance of subscript alignment
Shell Scripting
信息安全-威胁检测-flink广播流BroadcastState双流合并应用在过滤安全日志
Opencv learning log 27 -- chip positioning
Flag framework configures loguru logstore
QNetworkAccessManager实现ftp功能总结
The concept of C language array
socket通讯
Sword finger offer II 019 Delete at most one character to get a palindrome
Penetration test (1) -- necessary tools, navigation
Flask框架配置loguru日志庫