Modern mobile applications with Qt and QML
English French [qt-devnet.developpez.com]
Modern Mobile Applications with Qt and QML
Qt is a flexible and powerful framework for creating cross-platform applications. QML is now part of Qt, providing a markup language which gives complete freedom in development of user interfaces.
A good way to see the power of Qt is to start coding with it. Let’s write a simple application using Qt and QML. This will be an application I called 4Toddler; the application will start in full-screen mode and it has just two buttons placed in the top right corner of the window: About and Close. The main function is to display a random image with a fireworks effect and with a random sound effect.
The User interface will be written with QML; the backbone is written in Qt C++.
Create new project
If you do not have Qt already installed, visit Qt download page [qt.nokia.com] to download Qt SDK for your platform and install it.
Launch Qt Creator, select File -> New File or Project. In the new dialog select Qt C++ Project -> Qt Gui Application, and then click Choose…
In the new window: type project name, select path to the project folder and then click Next.
In the next window: uncheck Generate form option. We do not need to generate a form. Click Next
In the last window: just click Finish.
The application skeleton is ready, now we can proceed to code the logic and design the UI.
Code core logic in C++
First we need to add the qt-declarative [doc.qt.nokia.com] module to our project. This is the module that provides a widget in which a QML interface is displayed.
Open project file (4Toddler.pro) and append the line
with declarative
Now we need to change the base class for our main window. Replace QMainWindow with QDeclarativeView and include QDeclarativeView
Also we need to cut off QMainWindow(parent) from the MainWindow constructor; we do not need this initialization anymore.
If you launch the application right now you will see an empty window. This is because we have not initialized or created our QML interface yet.
Add QML interface
Let’s add a new file to our project: right click on the project in the projects explorer window.
Add New then select Qt section and Qt QML File in the templates list and click Choose…
Type the file name in the Name field, click Next then Finish in the next window.
The wizard will create and open the new QML file in the editor. There is just one Rectangle element inside it. This will be the root element for our user interface. Let’s add some new properties to this element.
Note: some versions of Qt Creator will add a Hello World text element centered in the Rectangle. Remove the text element before proceeding.
There is nothing special for now, just black background.
Multilingual
During the remainder of this tutorial we will shift back and forth between QML and C++ development.
We need to add some initialization code for our QML interface by defining a new method inside the mainwindow.h file …
.. And implementing it in the mainwindow.cpp file
Now we need to replace one line in the main.cpp file
With
This will force our window to open in full-screen mode.
Structure code with components
Before we launch our application, let’s add two buttons. One is to display About dialog and the second to Close our window. We do not need to implement buttons twice, we going to create a component [doc.qt.nokia.com] and use it every time we need to add a new button by overriding just a few properties.
Let’s add the new Qml file – WindowButton.qml to our project. Note the filename begins with a capital letter — this signifies it defines a QML component.
Now we can add two buttons to our window
Communicate between QML and C++
Now we should implement both callback() methods. To close the window we will call the Quit function of the main window. Now we will see how Qt and QML communicate. Methods will be implemented inside Qt and then called from the QML file.
Let’s add a new function to the mainwindow.h header file
And implementation in mainwindow.cpp file
Now we need to “tell” QML about this method. Inside Init we should to add just one line:
Now we can access window object’s functions declared as Q_INVOKABLE from QML. Window here is just an example, you could use any object name you want.
Let’s add callback() function implementation to close button
Visualize state and add animation
Ok, the application can be launched. Launch it and click the Close button. You see? State of the button is not changed after click; it looks like the button is just inactive. Let’s add state changes for normal and clicked states.
Item will change state if the condition described in when property is true. You could change state manually by assigning state property.
Let’s launch the application again. Ok, this time it looks more active. We can make our button nicer by adding some animation.
The Behavior is a simple and flexible way to create animations. This element allows you to specify a default animation for a property change.
Launch the application and try to click on About or Close buttons. This looks much better!
We will implement the About dialog only using QML. Dialog will appear on the screen when About button is clicked and disappear when the user clicks inside the button or on the background.
Let’s add About.qml file to our project.
Take a look at the line
As you can see, property can be assigned and can be calculated.
Let’s add About dialog and callback() function implementation for the button About. In Main.qml file we need to declare the new About element.
And callback() function implementation
to
Finally we need to implement the main functionality for our application.
The element to display a random icon on the screen will be an Image element. Let’s add a new file for the new element – Block.qml
Now we need to implement the keyboard handler. Handler will be implemented with JavaScript. Let’s add new main.js to our project.
As you see in code above we should implement new property randomIcon and method PlaySound.
Let’s add property declaration and method to access it to our mainwindow.h file.
Implementation in mainwindow.cpp file
Now we need to declare the method to play a random sound effect in mainwindow.h file
And implementation in mainwindow.cpp file
Almost done. All we need is to add the keyboard handler to our parent QML element. But first we should include main.js file to main.qml file
Keyboard event handler for root element
That’s all! Now you can launch the application and try to press any key on the keyboard. But… I almost forgot about the fireworks effect.
Let’s add a new file called Fireworks.qml. Why do we need a new file? Because this will be a reusable element, this will save time in the future.
Now we need to add our Firework element to the Block element. Open Block.qml file and add declaration of the new element
To launch fireworks on item going to visible state we need to add just one line of code
to “show” state
Launch application and press any key. Image appears on the screen with fireworks and sound effect.
This is a simple application, but this is a good playground to understand the principles of QML.
To learn more about QML you can extend this application by adding features. Who knows, maybe someday this sample will grow to a commercial app? Idea from me: educational software with letters, numbers, shapes, colors, etc.
***
This article originally appeared on the Intel AppUp(SM) developer program blog [appdeveloper.intel.com]. A Russian version is available here [habrahabr.ru]
Thanks to Dmitry [intelloware.com] for allowing us to copy his tutorial.