Creating a Qt Quick application
We already covered how to create a Qt (C++) application. How about giving the newly introduced Qt Quick application development a try? Qt Quick was introduced in Qt 4.8 and it is now becoming mature in Qt 5. Because the QML file is usually platform-independent, it enables you to develop an application for multiple targets, including mobile operating systems with the same code.
In this chapter, I'll show you how to create a simple Qt Quick application based on Qt Quick Controls 1.2, as follows:
- Create a new project named
HelloQML
. - Select Qt Quick Application instead of Qt Widgets Application, which we chose previously.
- Select Qt Quick Controls 1.2 when the wizard navigates you to Select Qt Quick Components Set.
Qt Quick Controls has been introduced since Qt 5.1 and is highly recommended because it enables you to build a complete and native user interface. You can also control the top-level window properties from QML. Getting confused by QML and Qt Quick?
Note
QML is a user interface specification and programming language. It allows developers and designers alike to create highly performant, fluidly animated, and visually appealing applications. QML offers a highly readable, declarative, JSON-like syntax with support for imperative JavaScript expressions combined with dynamic property bindings.
While Qt Quick is the standard library for QML, it sounds like the relation between STL and C++. The difference is that QML is dedicated to user interface design and Qt Quick includes a lot of visual types, animations, and so on. Before we go any further, I want to inform you that QML is different from C++ but similar to JavaScript and JSON.
Edit the main.qml
file under the root of the Resources
file, qml.qrc
, which Qt Creator has generated for our new Qt Quick project. Let's see how the code should be:
import QtQuick 2.3 import QtQuick.Controls 1.2 ApplicationWindow { visible: true width: 640 height: 480 title: qsTr("Hello QML") menuBar: MenuBar { Menu { title: qsTr("File") MenuItem { text: qsTr("Exit") shortcut: "Ctrl+Q" onTriggered: Qt.quit() } } } Text { id: hw text: qsTr("Hello World") font.capitalization: Font.AllUppercase anchors.centerIn: parent } Label { anchors { bottom: hw.top; bottomMargin: 5; horizontalCenter: hw.horizontalCenter } text: qsTr("Hello Qt Quick") } }
If you have ever touched Java or Python, the first two lines won't be too unfamiliar to you. It simply imports Qt Quick and Qt Quick Controls, and the number following is the version of the library. You may need to change the version if there is a newer library. Importing other libraries is a common practice when developing Qt Quick applications.
The body of this QML source file is actually in the JSON style, which enables you to understand the hierarchy of the user interface through the code. Here, the root item is ApplicationWindow
, which is basically the same thing as MainWindow
in the previous topics, and we use braces to enclose the statements just like in a JSON file. Although you could use a semicolon to mark an ending of a statement just like we do in C++, there is no need to do this. As you can see, the property definition needs a colon if it's a single-line statement and enclosing braces if it contains more than one subproperty.
The statements are kind of self explanatory and they are similar to the properties that we saw in the Qt Widgets application. A qsTr
function is used for internationalization and localization. Strings marked by qsTr
could be translated by Qt Linguist. In addition to this, you don't need to care about QString and std::string
any more. All the strings in QML are encoded in the same coding as the QML file and the QML file is created in UTF-8 by default.
As for the signals and slots mechanism in Qt Quick, it's easy if you only use QML to write the callback function to the corresponding slot. Here, we execute Qt.quit()
inside the onTriggered
slot of MenuItem
. It's viable to connect the signal of a QML item to a C++ object's slot, which I'll introduce later.
When you run this application in Windows, you can barely find the difference between the Text
item and the Label
item. However, on some platforms, or when you change the system font and/or its color, you'll find that Label
follows the font and the color scheme of the system, while Text
doesn't. Although you can use the properties of Text
to customize the appearance of Label
, it would be better to use the system settings to keep the looks of the application native. Well, if you run this application right now, it will appear similar to what is shown in the following screenshot:
Because there is no separate UI file for the Qt Quick applications, only a QML file, we use the anchors
property to position the items, and anchors.centerIn
will position the item in the center of the parent. There is an integrated Qt Quick Designer in Qt Creator, which could help you design the user interface of a Qt Quick application. If you need it, just navigate to Design mode when you're editing a QML file. However, I suggest you stay in Edit mode to understand the meaning of each statement.