-
Book Overview & Buying
-
Table Of Contents
Qt5 C++ GUI Programming Cookbook
By :
Next, we will learn how to put all the knowledge we learned in the previous example together and create a fake graphical login screen for an imaginary operating system. Style sheets are not the only thing you need to master in order to design a good UI. You will also need to learn how to arrange the widgets neatly using the layout system in Qt Designer.

objectName property of the label to currentDateTime and change its Text property to the current date and time just for display purposes, such as Monday, 25-10-2015 3:14 PM.restartButton and shutdownButton respectively.Horizontal Policy and Vertical Policy properties of the two buttons to Fixed and set the minimumSize property to 55x55. Then, set the text property of the buttons to empty as we will be using icons instead of text. We will learn how to place an icon in the button widgets in the following section.
Next, we will be adding the logo by using the following steps:
layoutTopMargin or layoutBottomMargin) to be temporarily bigger until a widget is added to the layout.logo. We will learn more about how to insert an image into the label to use it as a logo in the next section. For now, just empty out the text property and set both its Horizontal Policy and Vertical Policy properties to Fixed. Then, set the minimumSize property to 150x150.border: 1px solid;

Now let's create the login form by using the following steps:
layoutTopMargin property to a bigger number (that is, 100) so that you can add a widget to it more easily.layoutTopMargin to a number lower than that of the horizontal layout (that is, 20) so that we can place widgets in it.QWidget object, it will automatically inherit all the properties from the widget class, and so we are now able to adjust its size to suit our needs.QWidget object, which we just converted from the layout, to loginForm and change both its Horizontal Policy and Vertical Policy properties to Fixed. Then, set the minimumSize to 350x200.loginForm widget inside the horizontal layout, we can now set its layoutTopMargin property back to zero.loginForm widget to make it visible temporarily, except this time we need to add an ID selector in front so that it will only apply the style to loginForm and not its children widgets:#loginForm { border: 1px solid; }
We are not done with the login form yet. Now that we have created the container for the login form, it's time to put more widgets into the form:
text property of the upper label to Username: and the one below as Password:. Then, rename the two line edits as username and password respectively.text property to Login. After that, rename it as loginButton.sizeType property to Fixed and change the Height to 5.loginForm container and set all its margins to 35. This is to make the login form look better by adding some space to all its sides.Height property of the username, password, and loginButton widgets to 25 so that they don't look so cramped.
We're not done yet! As you can see, the login form and the logo are both sticking to the top of the main window due to the vertical spacer below them. The logo and the login form should be placed at the center of the main window instead of the top. To fix this problem, use the following steps:
sizeType property to Fixed and the Height property to 10.topPanel. The reason why the layout has to be converted into QWidget is that, we cannot apply style sheets to a layout, as it doesn't have any properties other than margins.centralWidget object from the object inspector window, which is right under the MainWindow panel, and set all the margin values to zero.
#centralWidget { background: rgba(32, 80, 96, 100); }#topPanel { background-color: qlineargradient(spread:reflect, x1:0.5, y1:0, x2:0, y2:0, stop:0 rgba(91, 204, 233, 100), stop:1 rgba(32, 80, 96, 100)); }border-radius property:#loginForm
{
background: rgba(0, 0, 0, 80);
border-radius: 8px;
}QLabel { color: white; }
QLineEdit { border-radius: 3px; }QPushButton
{
color: white;
background-color: #27a9e3;
border-width: 0px;
border-radius: 3px;
}hover:QPushButton:hover { background-color: #66c011; }
This example focuses more on the layout system of Qt. The Qt layout system provides a simple and powerful way of automatically arranging child widgets within a widget to ensure that they make good use of the available space.
The spacer items used in the preceding example help to push the widgets contained in a layout outward to create spacing along the width of the spacer item. To locate a widget to the middle of the layout, put two spacer items to the layout, one on the left side of the widget and another on the right side of the widget. The widget will then be pushed to the middle of the layout by the two spacers.
Change the font size
Change margin width
Change background colour