当前位置:网站首页>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 .
边栏推荐
猜你喜欢
浏览器打印边距,默认/无边距,占满1页A4
b站 實時彈幕和曆史彈幕 Protobuf 格式解析
921. Minimum additions to make parentheses valid
Data storage in memory & loading into memory to make the program run
Penetration test (4) -- detailed explanation of meterpreter command
1855. Maximum distance of subscript alignment
QWidget代码设置样式表探讨
顺丰科技智慧物流校园技术挑战赛(无t4)
Codeforces Round #801 (Div. 2)A~C
860. Lemonade change
随机推荐
C language must memorize code Encyclopedia
807. Maintain the urban skyline
Input can only input numbers, limited input
指定格式时间,月份天数前补零
Analyse du format protobuf du rideau en temps réel et du rideau historique de la station B
栈的经典应用—括号匹配问题
浏览器打印边距,默认/无边距,占满1页A4
It is forbidden to trigger onchange in antd upload beforeupload
1005. Maximized array sum after K negations
Codeforces Round #803 (Div. 2)A~C
628. Maximum product of three numbers
Common configuration files of SSM framework
[exercise 4-1] cake distribution
Codeforces Round #798 (Div. 2)A~D
Codeforces - 1526C1&&C2 - Potions
日期加1天
Borg maze (bfs+ minimum spanning tree) (problem solving report)
Quick to typescript Guide
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
Auto. Getting started with JS