Coffeegghttp: // www.cnblogs.com/coffeegg/archive/2011/11/15/2249452.htmlcallable official example http://doc.qt.nokia.com/4.3/stylesheet-examples.html.
Qt creator style sheet usage (QT creator) (stylesheet) (style) (qt4.7) (usage)
Abstract:
In QT create, style sheet is used to set the background, color, transparency, and other styles for buttons, windows, and other components, which can be directly written in the code, you can also right-click the change style sheet method in the UI Designer to set it. I am using the later method. The following describes several common usage methods.
Introduction:
The Development Environment used by the author is QT creator 201005 and qt4.7.
1. Use style sheet to Set background for the form
You should pay attention to using stylesheet to set the background image. If it is in mainwindow and dialog, right-click Change style sheet and choose background-image or border-image in Add Resource, select the image in the resource file (the former displays the image in pixels, and the latter can automatically scale the image according to the size of the form), for example:
However, the widget does not work. You will find that, in the same way, the background has not changed, but only the background image of its subform has changed.
In this case, we place a frame in the widget and set the background for the frame through stylesheet. All the parts in the following form are placed in this frame.
We know that the child form inherits the attributes of the parent form. That is to say, the background of the parent form also exists in the child form. How can we prevent the child form from inheriting the background of the parent form, in the edit style sheet, enter the following code:
# Desktop {
Border-image: URL (:/images/shorttop.jpg );
}
# Desktop *{
Border-image: URL ();
}
Desktop is your form name.
2. the menubar settings are transparent.
I want to create a menu button. Like the Ubuntu Application menu, a drop-down box will pop up when I click it. I chose mainwindow to implement it, however, how can I prevent menuba from being displayed on the top layer.
Set stylesheet of menuba,
background-color:transparent
Set the background to transparent.
3. The tool button does not display borders.
When we set the icon of the button, we found that the icon shape is not exactly the same as that of the button, for example:
Set stylesheet
border-style: flat;
The effect is as follows:
Note: You must select tool button instead of push button, because a dotted box is displayed during the push button activity.
In addition to border-style: flat, can the style be set to transparent? After it is set to transparent, the display effect is the same, but when it is pressed, the color of the pressed button is displayed if it is not covered by the image.
4. Set the attributes of child parts in stylesheet of the parent form
If there are multiple buttons and their styles are the same, do you need to set them one by one? No, we can set it in the parent form, so the child part will inherit its attributes.
If the style of a button is special, modify it separately. For example, set the following in stylesheet of the parent form:
QDialog{background:rgb(229, 255, 239)}
QMenuBar{background:url(:/image/header.bmp)}
QStatusBar{background:url(:/image/header.bmp)}
QPushButton{background:url(:/image/header.bmp)}
QLCDNumber{background:url(:/image/lcd.bmp)}
QLabel{color: rgb(0, 0, 255)}
QDial{background-color: rgb(67, 67, 67)}
QGroupBox {
border-width:2px;
border-style:solid;
border-color:#FFFFFF;
}
QGroupBox{color: rgb(255,255, 255)}
QToolButton{border-style: flat;}
Conclusion:
Style sheet is very convenient to use. The above are the summary of several usage, which will be further supplemented later.
How to Set stylesheet: 1. Designer method: directly Add the following lines to the stylesheet attribute: Color: red; Background-image: URL ("D:/back.jpg"); // set the font to red and set the background image ; Indicates coexistence; 2. Code method: setstylesheet (...) 3. If stylesheet is loaded in this way, the font size of the entire form may change, and the image loading is slow, seriously affecting the program execution speed. |
In addition, you can test the widget settings in a special way.
Http://blog.csdn.net/dbzhang800/article/details/6875352
Modify mainwindow. cpp as follows:
# Include "mainwindow. H"
# Include "ui_main1_1_h"
# Include <qfile> // Add these two header files
# Include <qtextstream>
Void loadstylesheet (qstring qssname) // Add the following function
{
Qfile data (qssname );
Qstring qssfile;
If (data. Open (qfile: readonly ))
{
Qtextstream stylein (& data );
Qssfile = stylein. readall ();
Data. Close ();
Qapp-> setstylesheet (qssfile );
}
}
Mainwindow: mainwindow (qwidget * parent ):
Qmainwindow (parent ),
UI (new UI: mainwindow)
{
Loadstylesheet (qstring (":/style1.qss"); // Add this sentence to the constructor to load stylesheet.
UI-> setupui (this );
}