当前位置:网站首页>Discussion on QWidget code setting style sheet
Discussion on QWidget code setting style sheet
2022-07-06 16:17:00 【Larry_ Yanan】
Preface
as everyone knows , stay QT Modify component style , It is set through style sheets . And set the style sheet , There are usually two ways , Respectively QT Designer Right click the interface directly to set the style sheet , And code setting style sheets .
Under normal circumstances, it can succeed , But in some cases, it is not successful , Let me tell you .
Comparison of two style sheet settings
The first one is ,QT Designer Right click the component directly in the interface , Enter .
Right click on background-color Set a color randomly inside , This is the simplest style sheet setting . intend , Set a blue background color .
About the syntax of style sheet statements , You can study slowly , In fact, there are many , Students who may contact the web are better at it .
For me , The most commonly used way of writing is :
#widgettop{
background-color: rgb(0, 170, 255);}
perhaps
#QWidget{
background-color: rgb(0, 170, 255);}
Why use #XXX{…} To include the original style statement ? The reason is like QWidget This kind of window container control , They are parents , The style settings will be automatically inherited to its subclasses . That is to say, if I go QWidget Put a QPushButton Button or QLabel label , They all inherit the parent class QWidget The style of , It's blue .
This is obviously not applicable in some cases . Of course, if subclasses repeatedly set style statements , Will override the style of the parent class , But in general #XXX{…} Words , It can ensure that the statement is only implemented in the parent class QWidget in , It will not affect its subclass controls .
Let's talk about widgettop and QWidget, The former is the object name of the control instance objectName, The latter is the class name of the control , Both can be set successfully , It can meet the effect of the above two paragraphs , That is, set blue .
Back to the beginning , In addition to the QT Designer Set the style sheet directly inside , You can also set in the code , for example :
ui->widgettop->setStyleSheet("#widgettop{background-color: rgb(255, 170, 127);}");
or
ui->widgettop->setStyleSheet("#QWidget{background-color: rgb(255, 170, 127);}");
Both of these can be set successfully , The effect achieved is orange :
Special case
After the style settings of the above two cases are clear , Let me talk about the special situation I encountered :
Some parameters in the style sheet , Contains some size information : For example, the border width of the window , Radius of corner fillet, etc , These values may depend on the resolution of the current display where the software window is located , Need flexibility .
such as , stay 1080P screen ,QWidget The fillet radius of is 10px, But in 4k On the screen , It needs to be displayed as 20px, To achieve the same viewing size .
therefore , You may encounter : Already in QT Designer Style sheets have been set inside , Then set the style sheet repeatedly in the code , The situation of .
Normally , Duplicate settings of style sheets , Will overwrite the original settings , So the above practice is also OK.
What is the problem ? yes QWidget It contains other controls !!
The problems that arise
Casually set a blue background Widget Lose one inside QLabel
Then set orange in the code
ui->widgettop->setStyleSheet("#widgettop{background-color: rgb(255, 170, 127);}");
The result is blue
hold QLabel Removed from the
Compile again , It's orange
This has nothing to do with the way the stylesheet is set , Namely QWidget Whether it contains the difference between other controls
Preliminary conclusion
QWidget As a container , And there are child controls inside ,QT Designer Style sheet settings of conflict with code settings , Need to cancel QT Designer Set up , It can only be set completely inside the code .
analysis
QWidget This class is quite strange , It is a container class , It is essentially used to hold things , But if there is nothing in it , It can be used as an independent window , If it has no parent class pointing , Even as an independent Windows window , and QMainWindow and QDialog The effect is the same , And I think it is more versatile and flexible , Therefore, the interface window generally adopts QWidget To do it .
But as analyzed above , There are many pits in this class . Not just this , At the top level where there is no parent class QWidget When setting the background of the window , Often unsuccessful , Need to be in paintEvent Overload inside , Set a piece of code , The style sheet will take effect
void XXX::paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
( Quote someone else's description :
If you want to subclass a QWidget, In order to be able to use style sheets , You need to provide paintEvent event . This is because QWidget Of paintEvent() It's empty. , And style sheets go through paint To be drawn into a window .)
Maybe that's why , It also leads to the problem of repeated setting of style sheets .
Because I was confused for a long time when setting the style , Wasted a lot of time , So try to analyze and record , But what specific reason did you say ?
I'm sorry , The level of strength is not very good , I really don't want to understand . You can give me some advice , Or wait for me to add later .
边栏推荐
猜你喜欢
树莓派4B64位系统安装miniconda(折腾了几天终于解决)
Sword finger offer II 019 Delete at most one character to get a palindrome
807. Maintain the urban skyline
Codeforces round 797 (Div. 3) no f
<li>圆点样式 list-style-type
antd upload beforeUpload中禁止触发onchange
QT按钮点击切换QLineEdit焦点(含代码)
第 300 场周赛 - 力扣(LeetCode)
Data storage in memory & loading into memory to make the program run
Codeforces Round #797 (Div. 3)无F
随机推荐
Openwrt source code generation image
Codeforces - 1526C1&&C2 - Potions
Bidirectional linked list - all operations
图图的学习笔记-进程
MariaDB的安装与配置
Configuration du cadre flask loguru log Library
“鬼鬼祟祟的”新小行星将在本周安全掠过地球:如何观看
VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
875. 爱吃香蕉的珂珂 - 力扣(LeetCode)
Sword finger offer II 019 Delete at most one character to get a palindrome
Vs2019 initial use
Interesting drink
b站 实时弹幕和历史弹幕 Protobuf 格式解析
The "sneaky" new asteroid will pass the earth safely this week: how to watch it
(POJ - 2739) sum of constructive prime numbers (ruler or two points)
树莓派4B安装opencv3.4.0
F - birthday cake (Shandong race)
MySQL grants the user the operation permission of the specified content
QT实现窗口渐变消失QPropertyAnimation+进度条
[exercise 4-1] cake distribution