In this example, we will learn how to change the look and feel of our program and make it look more professional by using style sheets and resources. Qt allows you todecorate yourgraphical user interfaces (GUIs) using a style sheet language calledQt Style Sheets, which is very similar toCascading Style Sheets (CSS), something that’s used byweb designersto decoratetheir websites.
How to do it...
Let’s get started by learning how to create a new project and get ourselves familiar withQt Designer:
- Open upQt Creator and create a new project. If this is the first time you have used Qt Creator, you can either click the big button, which readsCreate Project…, or simply go toFile |New Project….
- SelectApplication (Qt) from theProjects window and selectQtWidgets Application.
- Click theChoose... button at the bottom. A window will pop out and ask you to insert the project’s name andits location.
- ClickNext several times, then click theFinish button to create the project. We will stick with the default settings for now. Once the project has been created, the first thing you will see is a panel with tons of big icons on the left-hand side of the window, which is called the mode selector panel; we will discuss this in more detail in theDissecting QtDesigner recipe.
- You will see all your source files listed on the sidebar panel, which is located next to the mode selector panel. This is where you can select which file you want to edit. In this case, this is
mainwindow.ui
, because we are about to start designing theprogram’s UI. - Double-click the
mainwindow.ui
file; you will see an entirely different interface appear out of nowhere. Qt Creator helped you switch from the script editor to the UI editor (Qt Designer) because it detected the.ui
extension on the file you’re tryingto open. - You will also notice that the highlighted button on the mode selector panel has changed fromEdit toDesign. You can switch back to the script editor or change to any other tools by clicking one of the buttons located in the upper half of the modeselector panel.
- Let’s go back to Qt Designer and look at the
mainwindow.ui
file. This is the main window of our program (asthe filename implies) and it’s empty by default, without any widget on it. Youcan try to compile and run the program by pressing theRun button (the green arrow button) at the bottom of the mode selector panel; you will see an empty window pop up once the compilationis complete. - Let’s add a push button to our program’s UI by clicking on thePush Button item in theWidget Box area (under theButtons category) and dragging it to our main window in the form editor. Keep the push button selected; you will see all the properties of this button inside theProperty Editor area on the right-hand side of your window. Scroll down to the middle and look for a property calledstyleSheet. This is where you will apply styles to your widget, which may or may not be inherited from its children or grandchildren recursively, depending on how you set your style sheet. Alternatively, you can right-click on any widget in your UI at the form editor and selectChange styleSheet... from thepop-up menu.
- You can click on the input field of thestyleSheet property to directly write the style sheet code, or click on the… button beside the input field to open up theEdit Style Sheet window, which has a bigger space for writing longer code for style sheets. At the top of the window, you can find several buttons, such asAdd Resource,Add Gradient,Add Color, andAdd Font, that can help you kickstart your coding if you can’t remember the properties’ names. Let’s try to do some simple styling with theEdit StyleSheet window.
- ClickAdd Color and choosea color.
- Pick a random color from the color picker window – let’s say, a pure red color. Then,clickOK.
- A line of code has been added to the text field in theEdit Style Sheet window, which in my case isas follows:
color: rgb(255, 0, 0);
- Click theOK button; the text on your push button should changeto red.
How it works...
Let’s take a bit of time to get familiar with Qt Designer’s interface before we start learning how to design ourown UI:
Figure 1.1 – Overview of Qt Designer’s interface
The explanation for the preceding screenshot isas follows:
- Menu bar: The menu bar houses application-specific menus that provide easy access to essential functions, such as creating new projects, saving files, undoing, redoing, copying, and pasting. It also allows you to access development tools that come with Qt Creator, such as the compiler, debugger,and profiler.
- Widget Box: This iswhere you can find all the different types of widgets provided by Qt Designer. You can add a widget to your program’s UI by clicking one of the widgets from theWidget Box area and dragging it to theform editor.
- Mode selector: The mode selector is a side panel that places shortcut buttons for easy access to different tools. You can quickly switch between the script editor and form editor by clicking theEdit orDesign button on the mode selector panel, which is very useful for multitasking. You can also easily navigate to the debugger and profiler tools at the same speedand manner.
- Build shortcuts: The build shortcuts are located at the bottom of the mode selector panel. You can build, run, and debug your project easily by pressing the shortcutbuttons here.
- Form editor: The form editor is where you edit your program’s UI. You can add different widgets to your program by selecting a widget from theWidget Box area and dragging it to theform editor.
- Form toolbar: From here, you can quickly select a different form to edit. Click the drop-down box located at the top of theWidget Box area and select the file you want to open with Qt Designer. Beside the drop-down box are buttons to switch between the different modes of the form editor, and also buttons to change the layout ofyour UI.
- Object Inspector: TheObject Inspector area lists all the widgets within your current
.ui
file. All the widgets are arranged according to their parent-child relationship in the hierarchy. You can select a widget from theObject Inspector area to display its properties in thePropertyEditor area. - Property Editor: TheProperty Editor area will display all the properties of the widget you selected from either theObject Inspector area or the formeditor window.
- Action Editor andSignals & Slots Editor: This window contains two editors:Action Editor andSignals & Slots Editor. Both can be accessed from the tabs beneath thewindow.Action Editor is where you create actions that can be added to amenu bar or toolbar in yourprogram’s UI.
- Output panes: Output panes consist of several different windows that display information and output messages related to script compilation and debugging. You can switch between different output panes by pressing the buttons that carry a number before them, such as1 Issues,2 Search Results, or3Application Output.
There’s more...
In this recipe, we discussed how to apply style sheets to Qt widgets through C++ coding. Although that method works well, most of the time, the person who is in charge of designing the program’s UI is not the programmer, but rather a UI designer who specializes in designing user-friendly UI. In this case, it’s better to let the UI designer design the program’s layout and style sheet with a different tool and not mess around with the code. Qt provides an all-in-one editorcalledQt Creator.
Qt Creator consists of several different tools, such as a script editor, compiler, debugger, profiler, and UI editor. The UI editor, which is also calledQt Designer, is the perfect tool for designers to design their program’s UI without writing any code. This is because Qt Designer adopted thewhat you see is what you get approach by providing an accurate visual representation of the final result, which means whatever you design with Qt Designer will turn out the same visually when the program is compiledand run.
The similarities betweenQt Style Sheets and CSS areas follows:
As you can see, both of them contain a selector and a declaration block. Each declaration contains a property and a value, separated by a colon. In Qt, a style sheet can be applied to a single widget by calling theQObject::setStyleSheet()
function inC++ code.
Consider the following,for example:
myPushButton->setStyleSheet("color : blue");
The preceding code will turn the text of a button with themyPushButton
variable name to blue. You can achieve the same result by writing the declaration in the style sheet property field in Qt Designer. We will discuss Qt Designer more in theCustomizing basic stylesheets recipe.
Qt Style Sheets also supports all the different types of selectors defined in the CSS2 standard, including theuniversal selector,type selector,class selector, andID selector, which allows us to apply styling to a very specific individual widget orgroup of widgets. Forinstance, if we want to change the background color of aspecific line-edit widget with theusernameEdit
object name, we can do this by using an ID selector to referto it:
QLineEdit#usernameEdit { background-color:blue }
Note
To learn about all the selectors available in CSS2 (which are also supported by Qt Style Sheets), please refer to thisdocument:http://www.w3.org/TR/REC-CSS2/selector.html.