当前位置:网站首页>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,所以特意记录一下。
边栏推荐
- Perform general operations on iptables
- Suffix expression (greed + thinking)
- F - birthday cake (Shandong race)
- [exercise-9] Zombie's Treasury test
- China exterior wall cladding (EWC) market trend report, technical dynamic innovation and market forecast
- Opencv learning log 31 -- background difference
- X-forwarded-for details, how to get the client IP
- C language must memorize code Encyclopedia
- Common configuration files of SSM framework
- Opencv learning log 28 -- detect the red cup cover
猜你喜欢

信息安全-威胁检测引擎-常见规则引擎底座性能比较
Quick to typescript Guide
![[exercise-4] (UVA 11988) broken keyboard = = (linked list)](/img/59/78ca7170ab1fd364ec44cfbcdc7ab5.png)
[exercise-4] (UVA 11988) broken keyboard = = (linked list)

树莓派4B安装opencv3.4.0

Flask框架配置loguru日志庫

605. Planting flowers

【练习-7】Crossword Answers

Web based photo digital printing website

Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools
![[exercise-5] (UVA 839) not so mobile (balance)](/img/8e/48dcf75f7347b36301df6fc129c09d.png)
[exercise-5] (UVA 839) not so mobile (balance)
随机推荐
【练习-10】 Unread Messages(未读消息)
[exercise-7] crossover answers
Borg maze (bfs+ minimum spanning tree) (problem solving report)
1605. Sum the feasible matrix for a given row and column
Nodejs+vue online fresh flower shop sales information system express+mysql
Opencv learning log 13 corrosion, expansion, opening and closing operations
Opencv learning log 31 -- background difference
PySide6 信号、槽
Penetration test (2) -- penetration test system, target, GoogleHacking, Kali tool
对iptables进行常规操作
Common configuration files of SSM framework
信息安全-史诗级漏洞Log4j的漏洞机理和防范措施
【练习-8】(Uva 246)10-20-30==模拟
[exercise-2] (UVA 712) s-trees
双向链表—全部操作
[analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class
Raspberry pie csi/usb camera uses mjpg to realize web camera monitoring
[exercise-8] (UVA 246) 10-20-30== simulation
b站 實時彈幕和曆史彈幕 Protobuf 格式解析
1855. Maximum distance of subscript alignment