Use Setstylesheet to set the appearance of the graphical interface

Source: Internet
Author: User
Tags qt designer

Use Setstylesheet to set the appearance of the graphical interface

Use Setstylesheet to set the appearance of the graphical interface:
QT Style Sheets is a very advantageous tool that allows customizing the appearance of Windows,
It can also be done with subclass Qstyle, whose syntax is heavily derived from HTML CSS, but applies to Windows

Summarized:
Style Sheets is a textual setting that can use Qapplication::setstylesheet () or a window for the entire application to use Qwidget::setstylesheet (), If several style sheets are set at different levels,

QT will assemble all of the stylesheets to set the appearance, which is called cascade concatenation
For example: The following style sheet specifies that all qlineedit should be in yellow as their background color, and all check boxes should be in red as their text color
Qlineedit {Background:yellow}
Qcheckbox {color:red}

For this kind of customization, the stylesheet is more powerful than the palette palette, such as using Qpalette::button role to set a button red may cause danger

For customizations that are difficult to complete by using qpalette alone, style sheets can specify
The style sheet acts on top of the current window style, which means that the application speaks as naturally as possible, but any stylesheet system parameters should be considered, unlike Qpalette, where the stylesheet provides check if you set the background color of a button to red

Color, you should be sure that all the platform buttons will have a red background, in addition to this, Qt Designer provides a stylesheet integration environment that makes it easier to see style sheet effects in different window styles

In addition, style sheets can be used to provide a superior look to your application, without the use of subclass Qstyle, for example, you can specify any picture as a radio button and check button to make them stand out, use this technique, or you can get auxiliary

, this will use several subclasses, such as specifying style hint (style hint), which you can refer to in the example style Sheet.

When the style sheet is valid, use Qwidget::style () to return to Qstyle,

Style sheet Syntax:
The style sheet syntax is basically consistent with the HTML CSS syntax.
The style sheet contains a sequence of style rules with a <selector> and <declaration> composition,<selector> specifies which Windows will be affected by these rules,<declaration> Specifies which properties will be set on the window, for example
Qpushbutton{color:red}
In the above, the rule, Qpushbutton is <selector>,{color:red} is <declaration>, and this rule specifies that Qpushbutton and his subclasses will use red as the foreground color, that is, the font color , and there is no difference in capitalization, for

Color,color,color is the same.
Several <selector> can be listed at the same time, using the comma "," to separate each <selector>, for example:
Qpushbutton, Qlineedit, Qcombobox {color:red}
The <declaration> section is a pair of attributes: value pairs, enclosed in {}, using semicolons to separate attributes, such as
Qpushbutton {color:red; > see QT Style Sheets reference for a list of the parts and the properties of the style sheet

About cascading properties for style sheets
Look at the difference between the following code
Btn1->setstylesheet ("qpushbutton{color:red}"); Set foreground color, that is, 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 a yellow background, the second really red font, a yellow background,
So when setting up a component, write it completely in the same >setstylesheet ().

Source code Example:

Dialog::D ialog (Qwidget *parent):
Qdialog (parent),
UI (New UI::D ialog)
{
UI->SETUPUI (this);
This->setwindowflags (This->windowflags () &QT::WINDOWMAXIMIZEBUTTONHINT&QT:: Windowminimizebuttonhint);//Add the Maximize and Minimize buttons to the dialog box
Layout=new Qboxlayout (this);
Layout1=new Qgridlayout (this);
Btn1=new Qpushbutton (this);
Btn1->setstylesheet ("Qpushbutton{color:red;background:yellow}");//set foreground color, that is, font color
Btn1->setstylesheet ("Qpushbutton{background:yellow}");
Btn1->settext ("Button1");

Btn2=new Qpushbutton (this);
Btn2->setstylesheet ("qpushbutton{color:red;//uses 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-attachment:fixed;; Background-clip:padding} ");
    //Set the background image of the button, Background-repeat can set the background image of the repeating rules, where the setting is only repeated in the XY direction, so the picture will be repeated once
     //background-position is used to set the position of the picture, whether it is left or right, or in the Middle (center), on (top) or bottom (bottom)
    //background-attachment is used to scroll the background image or to match the window size, the default is scrolling
     btn3- >settext ("Button3");

     btn4=new Qpushbutton (this);
     btn4->setstylesheet ("qpushbutton{border:3px Solid red;border-radius:8px}");// Set border width and color
    //You can use Border-top,border-right,border-bottom,border-left to set the upper and lower borders of the buttons, respectively,
    //also have Border-left-color, Border-left-style, border-left-width, etc. to set their color, style and width
    //border-image The background image used to set the border. The
    //border-radius is used to set the radian of the border. You can set the button
     btn4->settext ("Button4") for rounded corners;

    //font settings
    //font-family to set the font family,
     //font-size to set font size
    //font-style to set font style
    // Font-weight to set the font depth
    //height to set its height
    // Selection-color is used to set the color of the selected time
     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 }");

Setting of the parent window
Icon-size to set the image size
This->setwindowicon (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 Widget main window of the CPP file, run the resulting results such as

We see that even the pasted copy board has become a style that is set using a style sheet.

For more information on using style sheet setstylesheet ()) To set the window style, refer to the

Http://blog.csdn.net/xie376450483/archive/2010/08/17/5818759.aspx

Use Setstylesheet to set the appearance of the graphical interface

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.