From: http://www.360doc.com/content/11/1122/10/7899729_166398154.shtml
Use setstylesheet to set the appearance of the graphic interface:
Qt style sheet is a good tool that allows you to customize the appearance of a window,
In addition, the subclass qstyle can also be used to complete the process. Its syntax is largely based on html css, but it is applicable to Windows.
Summary:
Style Sheets is a set of text. It can be used for the entire application.
Qapplication: setstylesheet () or a corresponding window can use qwidget: setstylesheet
(). If several style sheets are set at different levels,
Qt sets all style sheets to set the appearance, which is called cascading
For example, the following style sheet specifies that all qlineedit should use yellow as their background color.
The box should use red as their text color
Qlineedit {Background: yellow}
Qcheckbox {color: Red}
For this customization, style sheets are more powerful than the palette. For example, you can use qpalette: button role to set
Setting a button to Red may cause danger.
You can specify
The style sheet acts on the top of the current window style, which means the application looks as natural as possible, but any
The style sheet system parameters should be considered. Unlike qpalette, the style sheet provides a check. If you set
The background color of the button is red.
Color, you should be sure that there will be a red background in all the platform buttons, In addition, QT designer provides the style
The table integration environment makes it easier to see the effect of the style sheet in different window styles
In addition, style sheets can be used to provide an outstanding appearance for your applications, without the need to use subclass qstyle. For example
For example, you can specify any image as a single-choice button and a check button to make them stand out.
For assistance
This will use several subclasses, such as specifying style hint (style hint). See the style sheet example.
When a style table is valid, you can use qwidget: style () to return qstyle,
Style table Syntax:
The style sheet syntax is basically the same as the html css syntax.
A style sheet contains a sequence of style rules, which are composed of <selector> and <declaration>,
<Selector> specify which windows will be affected by these Rules, and <declaration> specify which properties will be set
In a window, for example
Qpushbutton {color: Red}
In the preceding rule, qpushbutton is <selector> and {color: Red} is <declaration>.
Then, the specified qpushbutton and its subclass use red as the foreground color, which is the font color and is case-sensitive.
No difference,
Color, color, and color are the same.
Several <selector> can be listed at the same time. Use commas (,) to separate each <selector>. For example:
Qpushbutton, qlineedit, qcombobox {color: Red}
<Declaration> some are a pair of attributes: value pairs, which are enclosed by {} and separated by semicolons,
For example
Qpushbutton {color: red; Background-color: White}
You can refer to QT style sheets reference to view the property list of parts and style sheets.
Cascade attributes of a style sheet
See the following code
Btn1-> setstylesheet ("qpushbutton {color: Red}"); // set the foreground color, that is, the font color.
Btn1-> setstylesheet ("qpushbutton {Background: yellow}"); // set the background color to red.
And
Btn1-> setstylesheet ("qpushbutton {color: red; Background: yellow }");
The first code can only display the yellow background, and the second is indeed the red font, the yellow background,
Therefore, when setting a part, it must be completely written in the same> setstylesheet.
Source code example:
Dialog: Dialog (qwidget * parent ):
Qdialog (parent ),
UI (new UI: Dialog)
{
UI-> setupui (this );
This-> setwindowflags (this-> windowflags ()
& QT: windowmaximizebuttonhint & QT: windowminimizebuttonhint); // Add a dialog box
Maximize and minimize buttons
// Layout = new qboxlayout (this );
Layout1 = new qgridlayout (this );
Btn1 = new qpushbutton (this );
Btn1-> setstylesheet ("qpushbutton {color: red; Background: yellow}"); // before setting
Scene color, that is, the font color
// Btn1-> setstylesheet ("qpushbutton {Background: yellow }");
Btn1-> settext ("button1 ");
Btn2 = new qpushbutton (this );
Btn2-> setstylesheet ("qpushbutton {color: red; Background-color: RGB
(200,155,100)} "); // use RGB to set the background color
Btn2-> settext ("button2 ");
Btn3 = new qpushbutton (this );
Btn3-> setstylesheet ("qpushbutton {background-image: URL
(Image/1.png); Background-repeat: Repeat-XY; Background-position:
Center; Background-Attachment: fixed; Background-
Attachment: fixed; Background-clip: padding }");
// Set the background image of the button. Background-repeat can be used to set repeated rules for the background image.
It must be repeated only in the XY direction, so the image will be repeated once.
// Background-position is used to set the image position, whether it is left or right
Center, top or bottom)
// Background-attachment is used to determine whether the background image is flushed or matches the window size.
Recognize as rolling
Btn3-> settext ("button3 ");
Btn4 = new qpushbutton (this );
Btn4-> setstylesheet ("qpushbutton {border: 3px solid red; border-
RADIUS: 8px} "); // you can specify the Border Width and color.
// You can use border-top, border-right, border-bottom, and border-left to set
Top, bottom, left, and right sides of the button,
// There are also border-left-color, border-left-style, and border-left-width.
Set their colors, styles, and widths
// Border-image is used to set the background image of the border.
// Border-radius is used to set the Radian of the border. Button with rounded corners
Btn4-> settext ("button4 ");
// Font setting
// Font-family to set the family to which the font belongs,
// Font-size to set the font size
// Font-style to set the Font Style
// Font-weight to set the font depth
// Height is used to set the height.
// Selection-color is used to set the selected color.
Edit1 = new qlineedit (this );
Edit1-> setstylesheet ("qlineedit {Font: bold italic large \" Times New
Roman \ "; font-size: 25px; color: RGB (55,100,255); Height: 50px; Border: 4px solid
RGB (155,200, 33); border-radius: 15px; selection-color: pink }");
// Set the parent window
// Icon-size to set the image size
This-> setmediawicon (qicon ("image/1.png "));
This-> setstylesheet ("qwidget {Background: Write URL (image/2.png); icon-
Size: 20px 5px} "); // sets the background color of the entire dialog box.
// This-> setstylesheet ("qwidget {icon-size: 20px 5px }");
Layout1-> addwidget (btn1, 0, 0 );
Layout1-> addwidget (btn2, 0, 1 );
Layout1-> addwidget (btn3, 1, 0 );
Layout1-> addwidget (btn4, 1, 1 );
Layout1-> addwidget (edit1, 2, 0 );
}
Here, only the CPP file in the main widget window is provided. The running result is as follows:
We can see that even the copy and paste board is changed to the style set using the style sheet.
For more information about how to use the style sheet setstylesheet () to set the window style, see
Http://blog.csdn.net/xie376450483/archive/2010/08/17/5818759.aspx