当前位置:网站首页>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,所以特意记录一下。
边栏推荐
- 【练习4-1】Cake Distribution(分配蛋糕)
- (POJ - 3685) matrix (two sets and two parts)
- Gartner:关于零信任网络访问最佳实践的五个建议
- Opencv learning log 26 -- detect circular holes and mark them
- What is the difficulty of programming?
- 树莓派CSI/USB摄像头使用mjpg实现网页摄像头监控
- 读取和保存zarr文件
- Opencv learning log 33 Gaussian mean filtering
- Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
- The most complete programming language online API document
猜你喜欢

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

Luogu P1102 A-B number pair (dichotomy, map, double pointer)

Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools

921. Minimum additions to make parentheses valid

【练习-7】Crossword Answers

Gartner:关于零信任网络访问最佳实践的五个建议

Flag framework configures loguru logstore

Differential (one-dimensional, two-dimensional, three-dimensional) Blue Bridge Cup three body attack

Borg maze (bfs+ minimum spanning tree) (problem solving report)

2027. Minimum number of operations to convert strings
随机推荐
Information security - Epic vulnerability log4j vulnerability mechanism and preventive measures
Opencv learning log 14 - count the number of coins in the picture (regardless of overlap)
969. Pancake sorting
Sanic异步框架真的这么强吗?实践中找真理
【练习-10】 Unread Messages(未读消息)
Raspberry pie csi/usb camera uses mjpg to realize web camera monitoring
Gartner:关于零信任网络访问最佳实践的五个建议
Penetration test (2) -- penetration test system, target, GoogleHacking, Kali tool
Sword finger offer II 019 Delete at most one character to get a palindrome
Web based photo digital printing website
[exercise-6] (UVA 725) division = = violence
Frida hook so layer, protobuf data analysis
Flask框架配置loguru日志库
Penetration test (8) -- official document of burp Suite Pro
JS call camera
Analysis of protobuf format of real-time barrage and historical barrage at station B
(POJ - 3579) median (two points)
B - Code Party (girls' competition)
Opencv learning log 15 count the number of solder joints and output
【练习-3】(Uva 442)Matrix Chain Multiplication(矩阵链乘)