Creating a Mobile Application
This tutorial describes developing Qt Quick applications for Android and iOS devices using Qt Quick Controls. We use Qt Creator to implement a Qt Quick application that accelerates an SVG (Scalable Vector Graphics) image based on the changing accelerometer values.
Setting up the Development Environment
To be able to build and run the application on a mobile device, you must set up the development environment for the device platform and configure a connection between Qt Creator and the mobile device.
To develop for Android devices, you must download and install the latest Android NDK and SDK, and update the SDK to get the API and tools needed for development. In addition, you must install the Java SE Development Kit (JDK) and Apache Ant. After you have installed all these tools, you must specify the paths to them in Qt Creator. For detailed instructions, see Qt for Android and Connecting Android Devices.
To develop for iOS devices, you must install Xcode and use it to configure a device. For this, you need an Apple developer account and iOS Developer Program certificate that you receive from Apple. For more information, see Connecting iOS Devices.
Creating the Project
- Select File > New File or Project > Application > Qt Quick Controls Application > Choose.
- In the Name field, enter a name for the application.
- In the Create in field, enter the path for the project files, and then select Next (or Continue on macOS).
- In the Minimal required Qt version field, select Qt 5.4, or later.
- Deselect the Enable native styling check box to keep the application size small. Enabling native styling would create a dependency to the Qt Widgets module.
- Select Next.
- Select kits for the platforms that you want to build the application for. To build applications for mobile devices, select kits for Android ARM and iPhone OS, and click Next.
Note: Kits are listed if they have been specified in Tools > Options > Build & Run > Kits (on Windows and Linux) or in Qt Creator > Preferences Build & Run > Kits (on macOS).
- Select Next.
- Review the project settings, and click Finish (or Done on macOS).
Qt Creator generates a UI file, MainForm.ui.qml, that you can modify in the Design mode and a QML file, main.qml, that you can modify in the Edit mode to add the application logic.
Creating the Accelbubble Main View
The main view of the application displays an SVG bubble image that moves around the screen when you tilt the device.
To use Bluebubble.svg in your project, copy it to the project directory (same subdirectory as the QML file). The image appears in Resources. You can also use any other image or a QML type, instead.
To create the UI in the Design mode:
- In the Projects view, double-click the MainForm.ui.qml file to open it in Qt Quick Designer.
- In the Navigator, select RowLayout and press Delete to delete it.
- In Library > QML Types, select Rectangle and drag and drop it to the Item in the navigator.
- Select the rectangle in the navigator to edit its properties:
- In the Id field enter mainWindow, to be able to reference the rectangle from other places.
- Select the Layout tab, and then click the (Fill to Parent) button to anchor the rectangle to the item.
- In Library > Resources, select Bluebubble.svg and drag and drop it to mainWindow in the navigator.
- In the Properties pane, Id field, enter bubble to be able to reference the image from other places.
- Select the (Export) button in the navigator to export the mainWindow and bubble as properties.
We want to modify the properties of the bubble in ways that are not supported by Qt Quick Designer, and therefore we create a custom QML type for it:
- Select Edit to open MainForm.ui.qml in the code editor.
- Right-click Image and select Refactoring > Move Component into Separate File.
- In the Component name field, enter Bubble and select OK to create Bubble.qml.
Qt Creator creates a reference to Bubble.qml in MainForm.ui.qml.
To check your code, you can compare MainForm.ui.qml with the MainForm.ui.qml example file and Bubble.qml with the Bubble.qml example file.
The UI is now ready and you can switch to editing the main.qml and Bubble.qml files in the Edit mode, as described in the following section.
Moving the Bubble
The new project wizard adds boilerplate code to the main.qml file to create menu items and push buttons. Modify the boilerplate code by removing obsolete code and by adding new code. You removed the push buttons from the UI Form, so you also need to remove the corresponding code from main.qml (or the application cannot be built).
In the code editor, edit Bubble.qml to add properties that we will use to position the image:
Image { source: "Bluebubble.svg" smooth: true property real centerX property real centerY property real bubbleCenter }
In the code editor, edit the main.qml to specify the application title, as illustrated by the following code snippet:
ApplicationWindow { id: mainWindow visible: true width: 640 height: 480 title: qsTr("Accelerate Bubble")
Use the bubble properties to position the image:
MainForm { anchors.fill: parent bubble { id: bubble centerX: mainWindow.width / 2 centerY: mainWindow.height / 2 bubbleCenter: bubble.width / 2
Then set the x and y position of the image based on the new properties:
x: bubble.centerX - bubble.bubbleCenter y: bubble.centerY - bubble.bubbleCenter }
Then add code to move the bubble based on Accelerometer sensor values:
- Add the following import statement to main.qml:
import QtSensors 5.5
- Add the Accelerometer type with the necessary properties:
Accelerometer { id: accel dataRate: 100 active: true }
- Add the following JavaScript functions that calculate the x and y position of the bubble based on the current Accelerometer values:
function calcPitch(x, y, z) { return -(Math.atan(y / Math.sqrt(x * x + z * z)) * 57.2957795); } function calcRoll(x, y, z) { return -(Math.atan(x / Math.sqrt(y * y + z * z)) * 57.2957795); }
- Add the following JavaScript code for
onReadingChanged
signal of Accelerometer type to make the bubble move when the Accelerometer values change:onReadingChanged: { var newX = (bubble.x + calcRoll(accel.reading.x, accel.reading.y, accel.reading.z) * 0.1) var newY = (bubble.y - calcPitch(accel.reading.x, accel.reading.y, accel.reading.z) * 0.1) if (isNaN(newX) || isNaN(newY)) return; if (newX < 0) newX = 0 if (newX > mainWindow.width - bubble.width) newX = mainWindow.width - bubble.width if (newY < 18) newY = 18 if (newY > mainWindow.height - bubble.height) newY = mainWindow.height - bubble.height bubble.x = newX bubble.y = newY }
We want to ensure that the position of the bubble is always within the bounds of the screen. If the Accelerometer returns not a number (NaN), the value is ignored and the bubble position is not updated.
- Add SmoothedAnimation behavior on the
x
andy
properties of the bubble to make its movement look smoother.Behavior on y { SmoothedAnimation { easing.type: Easing.Linear duration: 100 } } Behavior on x { SmoothedAnimation { easing.type: Easing.Linear duration: 100 } }
Locking Device Orientation
The device display is rotated by default when the device orientation changes between portrait and landscape. For this example, it would be better for the screen orientation to be fixed.
To lock the orientation to portrait or landscape on Android, specify it in an AndroidManifest.xml that you can generate in Qt Creator. For more information, see Editing Manifest Files.
On iOS, you can lock the device orientation in a Info.plist file that you specify in the .pro file as the value of the QMAKE_INFO_PLIST variable.
Adding Dependencies
Update the accelbubble.pro file with the following library dependency information:
QT += quick sensors svg xml
On iOS, you must link to the above libraries statically, by adding the plugin names explicitly as values of the QTPLUGIN variable. Specify a qmake scope for iOS builds (which can also contain the QMAKE_INFO_PLIST variable):
ios { QTPLUGIN += qsvg qsvgicon qtsensors_ios QMAKE_INFO_PLIST = Info.plist }
After adding the dependencies, select Build > Run qmake to apply the changes to the Makefile of the project.
Adding Resources
You need to add the Bluebubble.svg image file to the application resources for deployment to mobile devices:
- In the Projects view, double-click the qml.qrc file to open it in the resource editor.
- Select Add to add Bluebubble.svg.
Running the Application
The application is complete and ready to be deployed to a device:
- Enable USB Debugging on the Android device or developer mode on the iOS device.
- Connect the device to the development PC.
If you are using a device running Android v4.2.2, it should prompt you to verify the connection to allow USB debugging from the PC it is connected to. To avoid such prompts every time you connect the device, select the Always allow from the computer check box, and then select OK.
- To run the application on the device, press Ctrl+R.
Files:
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.