Creating a login screen using style sheets
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.
How to do it…
- The first thing we need to do is design the layout of the graphical login screen before we start doing anything. Planning is very important in order to produce good software. The following is a sample layout design I made to show you how I imagine the login screen will look. Just a simple line drawing like this is sufficient as long as it conveys the message clearly:
- Now that we know exactly how the login screen should look, let's go back to Qt Designer again.
- We will be placing the widgets at the top panel first, then the logo and the login form below it.
- Select the main window and change its width and height from 400 and 300 to 800 and 600 respectively because we'll need a bigger space in which to place all the widgets in a moment.
- Click and drag a label under the Display Widgets category from the widget box to the form editor.
- Change the
objectName
property of the label tocurrentDateTime
and change itsText
property to the current date and time just for display purposes, such asMonday, 25-10-2015 3:14 PM
. - Click and drag a push button under the Buttons category to the form editor. Repeat this process one more time because we have two buttons on the top panel. Rename the two buttons
restartButton
andshutdownButton
respectively. - Next, select the main window and click the small icon button on the form toolbar that says Lay Out Vertically when you mouse-over it. Now you will see the widgets are being automatically arranged on the main window, but it's not exactly what we want yet.
- Click and drag a horizontal layout widget under the Layouts category to the main window.
- Click and drag the two push buttons and the text label into the horizontal layout. Now you will see the three widgets being arranged in a horizontal row, but vertically they are located in the middle of the screen. The horizontal arrangement is almost correct, but the vertical position is totally off.
- Click and drag a vertical spacer from the Spacers category and place it below the horizontal layout we created previously (below the red rectangular outline). Now you will see all the widgets are being pushed to the top by the spacer.
- Now, place a horizontal spacer between the text label and the two buttons to keep them apart. This will make the text label always stick to the left and the buttons align to the right.
- Set both the
Horizontal Policy
andVertical Policy
properties of the two buttons toFixed
and set theminimumSize
property to55x55
. Then, set thetext
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. - Now your UI should look similar to this:
Next, we will be adding the logo by using the following steps:
- Add a horizontal layout between the top panel and the vertical spacer to serve as a container for the logo.
- After adding the horizontal layout, you will find the layout is way too thin in height to be able to add any widgets to it. This is because the layout is empty and it's being pushed by the vertical spacer below it into zero height. To solve this problem, we can set its vertical margin (either
layoutTopMargin
orlayoutBottomMargin
) to be temporarily bigger until a widget is added to the layout. - Next, add a label to the horizontal layout that you just created and rename it
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 thetext
property and set both itsHorizontal Policy
andVertical Policy
properties toFixed
. Then, set theminimumSize
property to150x150
. - Set the vertical margin of the layout back to zero if you haven't done so.
- The logo now looks invisible, so we will just place a temporary style sheet to make it visible until we add an image to it in the next section. The style sheet is really simple:
border: 1px solid;
- Now your UI should look similar to this:
Now let's create the login form by using the following steps:
- Add a horizontal layout between the logo's layout and the vertical spacer. Just as we did previously, set the
layoutTopMargin
property to a bigger number (that is, 100) so that you can add a widget to it more easily. - After that, add a vertical layout inside the horizontal layout you just created. This layout will be used as a container for the login form. Set its
layoutTopMargin
to a number lower than that of the horizontal layout (that is, 20) so that we can place widgets in it. - Next, right click the vertical layout you just created and choose Morph into -> QWidget. The vertical layout is now being converted into an empty widget. This step is essential because we will be adjusting the width and height of the container for the login form. A layout widget does not contain any properties for width and height, but only margins, due to the fact that a layout will expand toward the empty space surrounding it, which does make sense, considering that it does not have any size properties. After you have converted the layout to a
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. - Rename the
QWidget
object, which we just converted from the layout, tologinForm
and change both itsHorizontal Policy
andVertical Policy
properties toFixed
. Then, set theminimumSize
to350x200
. - Since we already placed the
loginForm
widget inside the horizontal layout, we can now set itslayoutTopMargin
property back to zero. - Add the same style sheet as the logo to the
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 tologinForm
and not its children widgets:#loginForm { border: 1px solid; }
- Now your UI should look something like this:
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:
- Place two horizontal layouts into the login form container. We need two layouts as one for the username field and another for the password field.
- Add a label and a line edit to each of the layouts you just added. Change the
text
property of the upper label toUsername:
and the one below asPassword:
. Then, rename the two line edits asusername
andpassword
respectively. - Add a push button below the password layout and change its
text
property toLogin
. After that, rename it asloginButton
. - You can add a vertical spacer between the password layout and the login button to distance them slightly. After the vertical spacer has been placed, change its
sizeType
property toFixed
and change theHeight
to5
. - Now, select the
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. - You can also set the
Height
property of theusername
,password
, andloginButton
widgets to 25 so that they don't look so cramped. - Now your UI should look something like this:
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:
- Add another vertical spacer between the top panel and the logo's layout. This way it will counter the spacer at the bottom which balances out the alignment.
- If you think that the logo is sticking too close to the login form, you can also add a vertical spacer between the logo's layout and the login form's layout. Set its
sizeType
property toFixed
and theHeight
property to10
. - Right click the top panel's layout and choose Morph into -> QWidget. Then, rename it
topPanel
. The reason why the layout has to be converted intoQWidget
is that, we cannot apply style sheets to a layout, as it doesn't have any properties other than margins. - Currently you can see there is a little bit of margin around the edges of the main window – we don't want that. To remove the margins, select the
centralWidget
object from the object inspector window, which is right under theMainWindow
panel, and set all the margin values to zero. - At this point, you can run the project by clicking the Run button (with the green arrow icon) to see what your program looks like now. If everything went well, you should see something like this:
- After we've done the layout, it's time for us to add some fanciness to the UI using style sheets! Since all the important widgets have been given an object name, it's easier for us to apply the style sheets to it from the main window, since we will only write the style sheets to the main window and let them inherit down the hierarchy tree.
- Right click on MainWindow from the object inspector window and choose Change Stylesheet.
- Add the following code to the style sheet:
#centralWidget { background: rgba(32, 80, 96, 100); }
- Now you will see that the background of the main window changes its color. We will learn how to use an image for the background in the next section, so the color is just temporary.
- In Qt, if you want to apply styles to the main window itself, you must apply it to its central widget instead of the main window itself because the window is just a container.
- Then, we will add a nice gradient color to the top panel:
#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)); }
- After that, we will apply black color to the login form and make it look semi-transparent. After that, we will also make the corners of the login form container slightly rounded by setting the
border-radius
property:#loginForm { background: rgba(0, 0, 0, 80); border-radius: 8px; }
- After we're done applying styles to the specific widgets, we will apply styles to the general types of widgets instead:
QLabel { color: white; } QLineEdit { border-radius: 3px; }
- The preceding style sheets will change all the labels' texts to a white color, which includes the text on the widgets as well because, internally, Qt uses the same type of label on the widgets that have text on it. Also, we made the corners of the line edit widgets slightly rounded.
- Next, we will apply style sheets to all the push buttons on our UI:
QPushButton { color: white; background-color: #27a9e3; border-width: 0px; border-radius: 3px; }
- The preceding style sheet changes the text of all the buttons to a white color, then sets its background color to blue, and makes its corners slightly rounded as well.
- To push things even further, we will change the color of the push buttons when we mouse-over it, using the keyword
hover
:QPushButton:hover { background-color: #66c011; }
- The preceding style sheet will change the background color of the push buttons to green when we mouse-over them. We will talk more about this in the following section.
- You can further adjust the size and margins of the widgets to make them look even better. Remember to remove the border line of the login form by removing the style sheet that we applied directly to it earlier.
- Now your login screen should look something like this:
How it works...
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.