In this example, we will learn how to use Qt Quick Designer to design our program's user interface.
main.qml
and MainForm.ui.qml
. The former is where we implement the logic for our application, and the latter is where we design our user interface. We will start with the UI design, so let's open up MainForm.ui.qml
. Once it's been opened by Qt Creator, you will see an entirely different UI editor compared to the one we used in previous chapters. This editor is called the Qt Quick Designer, which is used specifically to design UI for Qt Quick projects. The components of this editor are described as follows:border
property of the rectangles to 1
and the radius
to 5
. Then, set the echo mode
of one of the text fields to Password
.#bdbdbd
, then set its border
property to 1
and its radius
to 5
. Then, set the text
to Login
and make sure the size of the mouse area is the same as the rectangle.border color
to #5e5858
and its border
property to 2
. Then, set its radius
property to 5
to make its corners look a little rounded.main.qml
file. The widgets can be exported by clicking on the small icon behind the widget name and making sure the icon changes to the On status:main.qml
. Qt Creator will not open this file in Qt Quick Designer by default, but instead, it will be opened with the Script Editor. This is because all the UI design-related tasks were done in MainForm.ui.qml
, and main.qml
is only for defining the logic and functions that will be applied to the UI. You can, however, open it with Qt Quick Designer to preview the UI by clicking on the Design button located in the side bar on the left of the editor.main.qml
, like so:import QtQuick 2.5 import QtQuick.Window 2.2 import QtQuick.Dialogs 1.2
Window { visible: true width: 360 height: 360 MainForm { anchors.fill: parent loginButton.onClicked: { messageDialog.text = "Username is " + userInput.text + " and password is " + passInput.text messageDialog.visible = true } } MessageDialog { id: messageDialog title: "Fake login" text: "" onAccepted: { console.log("You have clicked the login button") Qt.quit() } } }
Since Qt 5.4, a new file extension called .ui.qml
has been introduced. The QML engine handles it like the normal .qml
files, but forbids any logic implementation to be written in it. It serves as the UI definition template, which can be reused in different .qml
files. The separation of UI definition and logic implementation improves the maintainability of QML code and creates a better workflow.
All the widgets under Qt Quick – Basic are the most basic widgets that we can use to mix and match and create a new type of widget. In the previous example, we have learned how to put three widgets together—a text, a mouse area, and a rectangle, to form a button widget.
If you're lazy, however, you can import pre-made modules to your Qt Quick project by going to the Imports tab in the Library window and clicking the <Add Import> button. Then, select the module you want to add to your project from the drop-down list. You can also create your own Qt Quick module once you have advanced in both QML scripting and C++ programming:
We imported QtQuick.dialogs
module in main.qml
and created a message box that displays the user name and password filled in by the user when the Login button is pressed, so that we can prove that the UI function is working. If the widgets are not exported from MainForm.ui.qml
, we will not be able to access its properties in main.qml
.
At this point, we can export the program to iOS and Android, but the UI may not look accurate on some of the devices that have higher resolution or higher Density-per-Pixel (DPI) unit. We will cover this issue later on in this chapter.
3.147.27.131