For a long time, the Qt has allowed the decorate your GUIs with CSS ' ish style sheets. Inspired by the web, stylesheets is a great a-to-stylize your Qt GUI, but it seems this few people use them. In this tutorial, we'll create an example dialog in Qt using Designer and stylesheets. This tutorial assumes so you can get the around in Qt Designer, and so understand a little about QT layouts.
Step 1 Create A new empty form, named Myloginform.
Step 2 Add A qframe to your form, and apply a vertical layout to your form (any layout would work actually:vertical, Horiz Ontal or Grid). Give your form about the pixels of layout margin around its perimeter. Name the Qframe "MainFrame". When dealing with stylesheets, it's convenient to the name your widgets in Designer, even if you don ' t plan to use them by Nam E in your code (we won ' t is writing any code besides CSS in this tutorial).
Step 3 in Designer's Object inspector, right-click on the top-level entry in the tree (called "Myloginform"), and select " Change StyleSheet ... "from the menu. You'll get a little editor dialog like this:
This is where we specify the style sheet for our form. You can put a style sheet on any widget in your form, but I prefer to does all my stylizing at the parent widget ("Myloginfo RM "in this case). I prefer to does it this is because you'll never has to go hunting to find your style sheet-it ' s all in one place R form. Since stylesheets Cascade down to the child widgets, you can stylize any widgets in your form from this point.
Side Note: "CSS" stands for "cascading Style Sheets"
Let's type in some CSS to the style sheet editor, like this:
#MyLoginForm {
background: gray;
}
#mainFrame {
border: 3px solid gray;
border-radius: 40px;
background: white;
}
After clicking OK on the Editor dialog, you should see this:
If your ' s doesn ' t look like this, the parent widget ' s name to ' Myloginform ' or the qframe of the forgotten to change ' s name to ' MainFrame ' (yes, capitalization does MATTER-QT stylesheets is case sensitive). mistyped something into the CSS dialog.
One cool feature is so you get to preview the style changes right as your make them. You don ' t have to compile, save, or run anything. Designer does a very good job of showing your stylesheet changes Live (WYSIWYG for you old-timers).
Let me explain what we just did. In the CSS, a pound sign, ' # ', in front of a name are how we stylize an individual widgets by that name. In we example, #MyLoginForm
identifies the parent widget (i.e., the background area). All we did there are give it a gray background with background: gray;
.
#mainFrame
for, we gave it a thick gray border, a white background, and rounded corners.
Step 4 Let's add some widgets to make this dialog actually do something. Drag and drop a pair of qlineedits, Qlabels, and a single qpushbutton on the form inside "MainFrame" and arrange them roug Hly like this:
Step 5 Now, apply a grid layout to "MainFrame". Just Select "MainFrame" by clicking on it (taking care not to accidentally select one of the Qlineedits or qlabels instead ). Then click on the Grid Layout button in Designer's toolbar (optionally, you can go to the menu bar and click "Form", "La Y out in a Grid ", or just press ctrl+5 for keyboard hackers).
Then give your layout some margin. I used pixels of margin and pixels for both vertical and horizontal spacing.
This is what you should has now:
Step 6 Let ' s stylize those boring Qpushbutton and qlineedits. Add this to the style sheet for Myloginform:
QLineEdit {
padding: 1px;
border-style: solid;
border: 2px solid gray;
border-radius: 8px;
}
QPushButton {
color: white;
border-width: 1px;
border-color: #339;
border-style: solid;
border-radius: 7;
padding: 3px;
font-size: 10px;
padding-left: 5px;
padding-right: 5px;
min-width: 50px;
max-width: 50px;
min-height: 13px;
max-height: 13px;
}
Notice that we didn ' t use the pound sign this time. When you omit the pound sign, your is specifying a "class" of widgets to Stylize instead of a single widget by name. We stylized all widgets of the type "Qlineedit" and "Qpushbutton" (and any widgets that may inherit from those Widgets too).
That gives the Qpushbutton a cool gradient look and rounds the edges of the qlineedits, like this:
Step 7 Now let's make this boring white background a gradient instead. Replace the "background:white;" line in the "#mainFrame" sections with this instead:
background: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #eef, stop: 1 #ccf);
Now your should see this:
Step 8 Since stylizing is all about detail and little tweaks, let's mess a bit with the label font and the background colo R by changing the background for ' #MyLoginForm ' to ' background:white ', and adding this:
QLabel {
font-weight: bold;
font-size: 15px;
}
Now we get our finished product:
Isn ' t it great how much the can do in Designer with style sheets? There is even things you can does with the style sheets that can ' t is done without writing lots of yucky C + + code. For example, you can change the border style of just a and a single side of a qframe.
Oddities
There is one gotcha to keep on mind when using style sheets:once your use a style sheet, it'll often override other prop Erties, like the ' font ' property or the ' FrameStyle ' property. Keep this on mind when Designer changes properties didn ' t expect.
The other oddity with style sheets are that there's no to "include" external style sheet files into your widget ' s style Sheet. This means the can ' t has a common style sheet that gets applied to your entire application like you can in the Web. I have a Qt patch of that allows-do-the-trolls haven ' t done anything with it (to my knowledge). This would is immensely useful.
Cross Platform Note
A style sheet tutorial wouldn ' t be complete without a comment on cross-platform issues. The dialog we just designed would look exactly the same on Linux, Windows, and Mac OS X because we stylized it so aggressiv Ely. If you want your widgets to look native on each platform, you should generally use style sheets sparingly, but if your Goa L is to has a consistent look across all platforms, style sheets was your best friend.
Conclusion
Now that I has discovered the power of style sheets in Qt, I use them whenever possible. What cool things are you doing with Qt style sheets?
References
Http://doc.qt.io/qt-4.8/stylesheet-examples.html
Http://www.cnblogs.com/cy568searchx/p/3645388.html
Http://www.cnblogs.com/davesla/archive/2011/01/30/1947928.html
http://blog.csdn.net/playstudy/article/details/8027892
QT Sample table for QT programming (QSS)