当前位置:网站首页>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,所以特意记录一下。
边栏推荐
- 628. Maximum product of three numbers
- b站 實時彈幕和曆史彈幕 Protobuf 格式解析
- 渗透测试 ( 3 ) --- Metasploit Framework ( MSF )
- Information security - security professional name | CVE | rce | POC | Vul | 0day
- Web based photo digital printing website
- 树莓派CSI/USB摄像头使用mjpg实现网页摄像头监控
- Ball Dropping
- 921. Minimum additions to make parentheses valid
- (POJ - 2739) sum of constructive prime numbers (ruler or two points)
- C basic grammar
猜你喜欢

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

Penetration test (8) -- official document of burp Suite Pro

Configuration du cadre flask loguru log Library

Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability

【练习-4】(Uva 11988)Broken Keyboard(破损的键盘) ==(链表)

【练习-7】Crossword Answers

Ball Dropping

渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具

X-Forwarded-For详解、如何获取到客户端IP

读取和保存zarr文件
随机推荐
【练习-2】(Uva 712) S-Trees (S树)
渗透测试 ( 8 ) --- Burp Suite Pro 官方文档
Opencv learning log 16 paperclip count
F - birthday cake (Shandong race)
C language is the watershed between low-level and high-level
Truck History
The concept of C language array
【高老师UML软件建模基础】20级云班课习题答案合集
Quick to typescript Guide
树莓派4B安装opencv3.4.0
What is the difficulty of programming?
Maximum product (greedy)
渗透测试 ( 1 ) --- 必备 工具、导航
Information security - threat detection - detailed design of NAT log access threat detection platform
1689. Ten - the minimum number of binary numbers
Opencv learning log 15 count the number of solder joints and output
[exercise-2] (UVA 712) s-trees
Opencv learning log 29 -- gamma correction
Write web games in C language
Luogu P1102 A-B number pair (dichotomy, map, double pointer)