creator-mobile-app-tutorial.qdoc 9.7 KB
Newer Older
1 2
/****************************************************************************
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
**
** This file is part of Qt Creator
**
**
** GNU Free Documentation License
**
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of this
** file.
**
**
****************************************************************************/

// **********************************************************************
// NOTE: the sections are not ordered by their logical order to avoid
// reshuffling the file each time the index order changes (i.e., often).
// Run the fixnavi.pl script to adjust the links to the index order.
// **********************************************************************

/*!
26
    \contentspage {Qt Creator Manual}
27
    \previouspage creator-writing-program.html
28
    \example accelbubble
Leena Miettinen's avatar
Leena Miettinen committed
29
    \nextpage {Using Qt Quick UI Forms}
30

31
    \title Creating a Mobile Application
32

33 34 35
    This tutorial describes developing Qt Quick applications for Android and iOS
    devices using Qt Quick Controls.
    We use \QC to implement a Qt Quick application
36 37 38 39 40
    that accelerates an SVG (Scalable Vector Graphics) image based on the
    changing accelerometer values.

    \image creator_android_tutorial_ex_app.png

41 42
    \section1 Setting up the Development Environment

43 44 45 46 47
    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 \QC and the mobile device.

    To develop for Android  devices, you must download and install
48
    the latest Android NDK and SDK, and update the SDK to get the API and tools
49
    needed for development. In addition, you must install the
50 51 52 53 54
    Java SE Development Kit (JDK) and Apache Ant. After you have installed all
    these tools, you must specify the paths to them in \QC.
    For detailed instructions, see \l{Qt for Android} and
    \l{Connecting Android Devices}.

55 56 57 58 59
    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
    \l{Connecting iOS Devices}.

60 61 62 63
    \section1 Creating the Project

    \list 1

64 65 66
        \li Select \uicontrol File > \uicontrol {New File or Project} >
            \uicontrol Application > \uicontrol {Qt Quick Controls Application}
            > \uicontrol Choose.
67

68
        \li In the \uicontrol Name field, type \e {accelbubble}.
69

70 71 72 73 74 75
        \li In the \uicontrol {Create in} field, enter the path for the project
            files, and then click \uicontrol Next (or \uicontrol Continue on
            OS X).

        \li In the \uicontrol {Minimal required Qt version} field, select the Qt
            version to develop with.
76

77 78 79 80
            \note This page determines the set of files that the wizard
            generates and their contents. The instructions in this tutorial
            might not apply if you select the \uicontrol {With .ui.qml file}
            check box.
81

82
        \li Select \l{glossary-buildandrun-kit}{kits} for Android ARM and iPhone
83
            OS, and click \uicontrol{Next}.
84

85 86 87 88
            \note Kits are listed if they have been specified in \uicontrol Tools >
            \uicontrol Options > \uicontrol {Build & Run} > \uicontrol Kits (on Windows and Linux)
            or in \uicontrol {Qt Creator} > \uicontrol Preferences \uicontrol {Build & Run} >
            \uicontrol Kits (on OS X).
89

90
        \li Select \uicontrol Next in the following dialogs to use the default
91 92
            settings.

93
        \li Review the project settings, and click \uicontrol{Finish} (or \uicontrol Done on
94
            OS X).
95

96 97
    \endlist

98 99
    \QC generates a default QML file that you can modify to create the main view
    of the application.
100 101 102 103 104 105

    \section1 Creating the Main View

    The main view of the application displays an SVG bubble image at the center
    of the main window.

106 107
    To use \l{accelbubble/Bluebubble.svg}{Bluebubble.svg} in your project,
    copy it to the project directory (same subdirectory as the QML file).
108
    The image appears in \uicontrol Resources. You can also use any other
109
    image or a QML type, instead.
110 111 112

    \list 1

113
        \li In the \uicontrol Projects view, double-click the main.qml file
114
            to open it in the code editor.
115

116
        \li Click \uicontrol Design to open the file in \QMLD.
117

118 119
        \li In the \uicontrol Navigator, select \uicontrol Label and press
            \key Delete to delete it.
120

121 122 123
        \li Select \uicontrol ApplicationWindow to edit its properties.

        \list a
124

125 126 127 128 129 130
            \li In the \uicontrol Id field, enter \e mainWindow, to be able to
                reference the window from other places.

            \li In the \uicontrol Title field, type \e {Accelerate Bubble}.

        \endlist
131

132
        \li In \uicontrol Library > \uicontrol Resources, select Bluebubble.svg
133
            and drag and drop it to the canvas.
134

135
        \li In the \uicontrol Properties pane, \uicontrol Id field, enter \e bubble to be
136
            able to reference the image from other places.
137

138
        \li In the code editor, add the following new properties to the image to
139
            position the image at the center of the application window when the
140
            application starts:
141

142
            \quotefromfile accelbubble/main.qml
143
            \skipto Image
144
            \printuntil bubble.width
145

146
         \li Set the x and y position of the image based on the new properties:
147 148

            \dots
149 150
            \printuntil centerY
            \skipto /^\}/
151 152
            \printuntil }

153
    \endlist
154

155
    For an example, see \l{accelbubble/main.qml}{main.qml}.
156 157 158 159 160 161 162

    \section1 Moving the Bubble

    Now that the visual elements are in place, let us move the bubble based on
    Accelerometer sensor values.

    \list 1
163
        \li Add the following import statement to main.qml:
164

165 166 167
            \code
            import QtSensors 5.0
            \endcode
168

169
        \li Add the \l{Accelerometer} type with the necessary properties:
170

171
            \quotefromfile accelbubble/main.qml
172 173 174 175 176 177 178 179 180
            \skipto Accelerometer
            \printuntil true
            \skipto }
            \printuntil }

        \li Add the following JavaScript functions that calculate the
            x and y position of the bubble based on the current Accelerometer
            values:

181 182 183 184
            \quotefromfile accelbubble/main.qml
            \skipto function
            \printuntil Math.atan(x
            \printuntil }
185 186 187 188 189

        \li Add the following JavaScript code for \a onReadingChanged signal of
            Accelerometer type to make the bubble move when the Accelerometer
            values change:

190 191 192
            \quotefromfile accelbubble/main.qml
            \skipto onReadingChanged
            \printuntil }
193

194 195 196
            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.
197 198 199
        \li Add SmoothedAnimation behavior on the \a x and \a y properties of
            the bubble to make its movement look smoother.

200 201 202 203 204
            \quotefromfile accelbubble/main.qml
            \skipto Behavior
            \printuntil x
            \printuntil }
            \printuntil }
205 206
     \endlist

207 208 209 210 211 212 213 214 215 216 217 218 219
     \section1 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 \QC. For more information,
    see \l{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.

220
    \section1 Adding Dependencies
221

222 223
    Update the accelbubble.pro file with the following library dependency
    information:
224

225 226 227
    \code
    QT += quick sensors svg xml
    \endcode
228

229 230 231 232 233 234 235 236 237 238 239 240
    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):

    \code
    ios {
    QTPLUGIN += qsvg qsvgicon qtsensors_ios
    QMAKE_INFO_PLIST = Info.plist
    }
    \endcode

241
    After adding the dependencies, select \uicontrol Build > \uicontrol {Run qmake} to apply
242 243 244 245 246 247 248 249 250
    the changes to the Makefile of the project.

    \section1 Adding Resources

    You need to add the Bluebubble.svg image file to the application resources
    for deployment to mobile devices:

    \list 1

251
        \li In the \uicontrol Projects view, double-click the qml.qrc file to open it
252 253
            in the resource editor.

254
        \li Select \uicontrol Add to add Bluebubble.svg.
255 256 257

    \endlist

258
    \section1 Running the Application
259

260
    The application is complete and ready to be deployed to a device:
261

262
    \list 1
263

264 265
        \li Enable \e{USB Debugging} on the Android device or \e{developer mode}
            on the iOS device.
266

267
        \li Connect the device to the development PC.
268 269 270

    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
271 272 273
    to. To avoid such prompts every time you connect the device, select the
    \uicontrol {Always allow from the computer} check box, and then select
    \uicontrol OK.
274

275
        \li To run the application on the device, press \key {Ctrl+R}.
276

277
    \endlist
278
*/