Precautions for setting widget background color/image in QT (use the style sheet setstylesheet ())
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 !! (You can use label or frame to display the image as the background)
②: 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! (To prevent style inheritance, you can only reset your own style or * {image: URL ();})
③: 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 code of the previous example:
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 );
}
Run the following command:
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:
It can be seen that once the style sheet is set in the top-level window, its Child Window will not take effect no matter how it is set to 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 );
}
The running effect is the same as above. This indicates that no matter 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 window does not match the size of the widget window, we cannot use a program to scale the image, unless we directly process the image to make it size consistent with the widget window; if it is not limited to the setstylesheet style sheet, we can choose the qpalette color edition, before its internal setbrush, we can scale the image to the window first, 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, see the articleHttp://blog.csdn.net/NRC_DouNingBo/archive/2010/05/09/5571149.aspx