-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Qt5 C++ GUI Programming Cookbook
By :
Qt Meta Language or Qt Modeling Language (QML) is a Javascript-inspired user interface mark-up language used by Qt for designing user interfaces. Qt provides you with Qt Quick components (widgets powered by the QML technology) to easily design touch-friendly UI without C++ programming. We will learn more about how to use QML and Qt Quick components to design our program's UI by following the steps given in the following section.
.qml files, namely main.qml and MainForm.ui.qml, inside the project resource. These two files are the UI description files using the QML mark-up language. If you double click main.qml file, Qt Creator will open up the script editor and you will see something like this: import QtQuick 2.5
import QtQuick.Window 2.2
Window {
visible: true
MainForm {
anchors.fill: parent
mouseArea.onClicked: {
Qt.quit();
}
}
}MainForm which is actually from the other .qml file called MainForm.ui.qml. It also tells Qt that when the user clicks on the mouseArea widget, the entire program should be terminated. MainForm.ui.qml file by double-clicking on it. This time, Qt Designer (UI editor) will be opened instead, and you will see a completely different UI editor compared to the C++ project we did previously. This editor is also called the Qt Quick Designer, specially designed for editing QML-based UI only. main.cpp file in your project, you will see this line of code: QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));main.qml file when the program starts. If you want to load the other .qml file instead of main.qml, you know where to look for the code. main.qml is loaded by the QML engine, it will also import MainForm.ui.qml into the UI, since MainForm is being called in the main.qml file. Qt will check if MainForm is a valid UI by searching for its .qml file based on the naming convention. Basically the concept is similar to the C++ project we did in the previous section, whereby the main.qml file acts like the main.cpp file and MainForm.ui.qml acts like the MainWindow class. You can also create other UI templates and use them in main.qml. Hopefully this comparison will make it easier to understand how QML works. MainForm.ui.qml. You should see three items listed on the navigator window: Rectangle, mouseArea, and Text. When these items are interpreted by the QML engine, it produces the following result on the canvas: 
centralWidget we used in the previous section. The mouseArea item is an invincible item that gets triggered when the mouse is clicking on it, or when a finger is touching it (for mobile platforms). The mouse area is also used in a button component, which we will be using in a while. The Text component is self-explanatory: it is a label that displays a block of text on the application.
You can pan around the canvas view by holding the middle mouse button (or mouse scroll) while moving your mouse around. You can also zoom in and out by scrolling your mouse while holding the Ctrl key on your keyboard. By default, scrolling your mouse will move the canvas view up and down. However, if your mouse cursor is on top of the horizontal scroll bar of the canvas, scrolling the mouse will move the view to the left and right.
800x600, as we're going to need a bigger space for the widgets.main.qml and remove these lines of code:mouseArea.onClicked: {
Qt.quit();
}This is because the mouseArea item no longer exists and it will cause an error when compiling.
MainForm.ui.qml:property alias mouseArea: mousearea

Color property of the top panel and select Gradient mode. Set the first color to #805bcce9 and the second color to #80000000. This will create a half-transparent panel with a blue gradient. 50x50. Then, make it a child of the top panel by dragging it on top of the top panel in the navigator window. #27a9e3) and set its radius to 2 to make its corners slightly rounded. Then, enable top anchor and right anchor to make it stick to the top right corner of the window. Set the top anchor's margin to 8 and right anchor's margin to 10 to give out some space. 

360x200 and set its radius to 15. #80000000, which will change it to black with 50% transparency.100 so that it moves slightly lower to the bottom to give space to the logo. The following screenshot illustrates the settings of the anchors:
Username: and Password: respectively. Then, change their text color to white and position them accordingly. We don't need to set a margin this time because they will follow the rectangle's position.5 to give them some rounded corners. After that, enable fill anchors on both of the rectangles so that they will follow the size of the text input widgets.#27a9e3) and enable the fill anchor so that it fits nicely with the mouse area. Login. Finally, enable the horizontal center anchor and the vertical center anchor to align it to the center of the button. 
512x200.
Qt Quick editor uses a very different approach for placing widgets in the application compared to the form editor. It's entirely up to the user which method is best suited for him/her.
The following screenshot shows what the Qt Quick Designer looks like:

We will now look at the various elements of the editor's UI:
Change the font size
Change margin width
Change background colour