QT Style Sheet usage notes

Source: Internet
Author: User

QT Style Sheet usage notes

<1>. Use of stylesheet
The default suffix of the stylesheet file is QSS, and you can set the style sheet-stylesheet filename.qss by command-line arguments, or by Qapplication::setstylesheet or qwidget: Setstylesheet to set the style sheet to be used by an application or a specific control
:: The parameter of the Setstylesheet function is a string (not the name of the QSs file, but the content of the stylesheet), so it is not convenient to set up a large number of rules at once, but you can use a resource file to embed the QSS style sheet into the program and then qapplication: Setstylesheet to use, for example:
QFile file (":/qss/mystylesheet.qss");
File.Open (qfile::readonly);
App.setstylesheet (File.readall ());

<2>. style sheet Description Rules
For example:
Qcombobox#mycombobox::d own-arrow:pressed{
position:relative;
top:1px; left:1px;
}
which
Qcombobox#mycombobox This section is called the type selector, and Qcombobox specifies the object class name, #name指定对象的实例名 (not required)
The Down-arrow child Control Descriptor (Subcontrol), and the preceding fields are separated by:: This represents the drop-down button of the combo box
The pressed pseudo-state (pseudo-states) descriptor, and the preceding fields are separated by: This indicates the state of the press
Above except for the first field, it is not necessary, but further restrict the scope of the rule, attribute definition, by the paired property name: attribute values, separated.

<3> priority relationship
A more specific type descriptor defines a rule with a higher priority, for example (the text color of a button with a mouse hover is white, otherwise red):
Qpushbutton:hover {Color:white}
Qpushbutton {color:red}

Rules that are defined after precedence are higher than the preceding rules, for example (in this case the color is red):
Qpushbutton:hover {Color:white}
qpushbutton:enabled {color:red}

Style sheets do not consider inheriting precedence relationships for classes, such as (this case color is gray):
Qpushbutton {color:red}
Qabstractbutton {Color:gray}

The style sheet for the control itself takes precedence over the parent control or the application's style sheet, for example:
Qapp->setstylesheet ("Qpushbutton {color:white}");
Mypushbutton->setstylesheet ("* {color:blue}");

<4>. style sheet inheritance
In a standard CSS style sheet, the font and color properties of a control are automatically inherited from the parent control if it is not explicitly set, and in the QT style sheet, it is not automatically inherited, for example:
Qapp->setstylesheet ("Qgroupbox {color:red;}");
In this case, for the added child controls in GroupBox, their color properties are not automatically set, and if you want to set the properties of the child controls, you need to explicitly set:
Qapp->setstylesheet ("Qgroupbox, Qgroupbox * {color:red;}");
Note that the inheritance here is not the same as the cascade above, which refers to different style sheets setting the same object, and inheritance refers to the effect of selectors on the control hierarchy in a style sheet rule.

QT Style Sheet usage notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.