Precautions for setting widget background color/image in QT (use the style sheet setstylesheet ())

Source: Internet
Author: User
Tags ibutton

There are many ways to set the widget background color or image in QT: override paintevent (), the color palette qpalette, and the style sheet setstylesheet.

However, all methods have their own precautions. If you do not pay attention to them, it is easy to get into trouble.

 

1: setstylesheet ()

I have always liked this function, because it is easier to write a sentence, but it is worth noting, that is to say, let me have a big head.

That is:①: This function can only be used to set the background of a child window with a parent window! If a window does not have a subwindow, you cannot use this function to set the background color or image !!

②: At the same time: for a parent window: If we use setstyleshette to set its style, but for its subwindows: if we do not use the same function to set it, the style of its child window is exactly the same as that of its parent window. That is, it integrates the style of its parent window!

③: For the top-level window (without a parent window), there are several child windows. When we use setstyleshette to set the style of the top-level window, we can see from ①: the parent window does not change, that is, the settings do not take effect. Its Child Window: as long as the Child Window does not use setstyleshette to set its own style sheet, then it is the style sheet of its parent window !!

 

For example:

The main window (without a parent class) is mainwin

Mainwin: mainwin ()

{

This-> setstylesheet ("background-image: URL (:/BMP/img_0345.jpg )");

IButton = new qpushbutton (this );

Ilabel = new qlabel (iButton );

}

 

Run the following command:

That is, although we set the top-level parent window, the style takes effect in its child window, and the top-level parent window does not change! This verifies ①.

 

 

Modify the previous exampleCode:

 

Mainwin: mainwin ()

{

This-> setstylesheet ("background-image: URL (:/BMP/img_0345.jpg )");

IButton = new qpushbutton (this );

Ilabel = new qlabel (iButton );

Ilabel-> setstylesheet ("background-image: URL (:/BMP/1257253475842.jpg )");

}

The running effect is as follows:

Visible: the Child Window can only use setstylesheet () to set its own style to partition the style of the parent window. Otherwise, it will use the style of the parent window.

 

Modify the Code:

Mainwin: mainwin ()

{

IButton = new qpushbutton (this );

IButton-> setstylesheet ("background-image: URL (:/BMP/img_0345.jpg )");

Ilabel = new qlabel (iButton );

}

 

RunThe effect is as follows:

 

Visible: setstylesheet () takes effect when setting a child window with a parent window !!!

 

 

Later, I thought about another problem: for the top-level parent window, if I set a style sheet for setstylesheet () and for its child windows, I used other methods, for example, if you use the qpalette palette to set the background image/color, is the background of the Child Window determined by the style table inherited from the parent window or by the qpalette palette of the Child Window itself?

 

Modify the code again:

Mainwin: mainwin ()

{

This-> setstylesheet ("background-image: URL (:/BMP/img_0345.jpg )");

 

IButton = new qpushbutton (this );

Ilabel = new qlabel (iButton );

 

 

Qpalette palette;

Palette. setbrush (ilabel-> backgroundrole (), qbrush (qimage (":/BMP/1257253475842.jpg ")));

Ilabel-> setpalette (palette );

Ilabel-> setautofillbackground (true );

 

}

 

In this Code segment, I use qpalette to set the background image of the sub-window to see whether the style sheet or the color palette takes effect. The effect is as follows:

ThereforeVisible: Once the style sheet is set in the top-level window, its subwindows will not take effect no matter how you set the background !!!

 

What if a style sheet is set in a common window instead of a top-level window? Modify the code again:

 

Mainwin: mainwin ()

{

 

IButton = new qpushbutton (this );

IButton-> setstylesheet ("background-image: URL (:/BMP/img_0345.jpg )");

Ilabel = new qlabel (iButton );

 

 

Qpalette palette;

Palette. setbrush (ilabel-> backgroundrole (), qbrush (qimage (":/BMP/1257253475842.jpg ")));

Ilabel-> setpalette (palette );

Ilabel-> setautofillbackground (true );

 

}

 

RunThe effect is the same as above. This indicates that, whether it is a top-level window or a general window, as long as the style sheet is set with setstylesheet, the background color/image of its subwindows will not take effect in other ways, you can only set changes in the same way as setstylesheet !!!

 

To verify the above conclusion, modify the code again:

Mainwin: mainwin ()

 

 

IButton = new qpushbutton (this );

Ilabel = new qlabel (iButton );

 

 

Qpalette palette;

Palette. setbrush (ilabel-> backgroundrole (), qbrush (qimage (":/BMP/1257253475842.jpg ")));

Ilabel-> setpalette (palette );

Ilabel-> setautofillbackground (true );

 

}

 

Run:

 

In this case, the color palette takes effect, which indirectly proves the above conclusion.

 

========================================================== ========================================

Summary:

1: Do not use setstylesheet () in the top-level window (window without a parent class). Otherwise, the background of its parent window will not change, and the background setting method of its child window will become limited and unique, you cannot use other methods!

2: if a common window (non-top-level window) has sub-Windows, it is best not to use setstylesheet () to set the background color, because the background settings of the window are effective at this time, however, the background settings of its subwindows become restricted and unique. You can only use setstylesheet, but not other methods! Of course: If you only want to use this method, you can do it !!

 

 

To put it bluntly, do not use setstylesheet () in mainwindow ()!

The preceding section emphasizes broadening the range of background settings in the sub-window. This is because, if you can only set a background image using the setstylesheet style sheet, the image cannot be scaled, if the size of the widget does not match the size of the widget windowProgramTo scale the image, unless we directly process the image to make its size consistent with the widget window. If it is not limited to the setstylesheet style sheet, we can choose to use the qpalette color edition, before its internal setbrush (), we can scale the image first and then brush it to the window, so that we can avoid directly processing the image, which is more flexible!

========================================================== ========================================

 

Note: This document only applies to the qwidget system and does not apply to the qgraphicswidget system! For the distinction between the two systems, seeArticleHttp://blog.csdn.net/NRC_DouNingBo/archive/2010/05/09/5571149.aspx

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.