-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Qt 6 C++ GUI Programming Cookbook
By :

In the previous recipe, you learned how to apply a style sheet to a widget with Qt Designer. Let’s go crazy and push things further by creating a few other types of widgets and changing their style properties to something bizarre for the sake of learning.
This time, however, we will not apply the style to every single widget one by one; instead, we will learn to apply the style sheet to the main window and let it inherit down the hierarchy to all the other widgets so that the style sheet is easier to manage and maintain in the long run.
In the following example, we will format different types of widgets on the canvas and add some code to the style sheet to change its appearance:
PushButton
by selecting it and clicking the small arrow button beside the styleSheet property. This button will revert the property to its default value, which in this case is the empty style sheet.Figure 1.2 – Dragging and dropping some widgets onto the form editor
border: 2px solid gray; border-radius: 10px; padding: 0 8px; background: yellow;
QPushButton { border: 2px solid gray; border-radius: 10px; padding: 0 8px; background: yellow; }
Figure 1.3 – Changing the push buttons to yellow
QPushButton
class. We can also apply the style to just one of the push buttons by mentioning its name in the style sheet, as shown in the following code:QPushButton#pushButton_3 { border: 2px solid gray; border-radius: 10px; padding: 0 8px; background: yellow; }
QPushButton { color: red; border: 0px; padding: 0 8px; background: white; } QPushButton#pushButton_2 { border: 1px solid red; border-radius: 10px; }
pushButton_2
button. We keep the style sheet of pushButton_3
as-is. Now, the buttons will look like this:Figure 1.4 – Applying a different style to each button
QPushButton
type to a white rectangular button with no border and red text. The second set of style sheets only changes the border of a specific QPushButton
widget called pushButton_2
. Notice that the background color and text color of pushButton_2
remain white and red, respectively, because we didn’t override them in the second set of style sheets, hence it will return to the style described in the first set of style sheets since it applies to all the QPushButton
widgets. The text of the third button has also changed to red because we didn’t describe the Color property in the third set of style sheets.* { background: qradialgradient(cx: 0.3, cy: -0.4, fx: 0.3, fy: -0.4, radius: 1.35, stop: 0 #fff, stop: 1 #888); color: rgb(255, 255, 255); border: 1px solid #ffffff; }
rgb
function (rgb(255, 255, 255)
) or hex code (#ffffff
) to describe the color value.Figure 1.5 – Applying a gradient background to all the other widgets
If you are ever involved in web development using HTML and CSS, Qt’s style sheets work the same way as CSS. Style sheets provide the definitions to describe the presentation of the widgets – what the colors are for each element in the widget group, how thick the border should be, and so on. If you specify the name of the widget to the style sheet, it will change the style of the particular PushButton
widget with the name you provide. None of the other widgets will be affected and will remain as the default style.
To change the name of a widget, select the widget from either the form editor or the Object Inspector area and change the objectName property in the property window. If you used the ID selector previously to change the style of the widget, changing its object name will break the style sheet and lose the style. To fix this problem, simply change the object name in the style sheet as well.