summaryrefslogtreecommitdiff
path: root/doc/src/android/creator-android-app-tutorial.qdoc
blob: 40ff22423502b68da84cde12cb6ff55dd153e8fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/****************************************************************************
**
** Copyright (c) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** 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.
// **********************************************************************

/*!
    \contentspage index.html
    \previouspage creator-writing-program.html
    \page creator-android-app-tutorial.html
    \nextpage creator-project-managing.html

    \title Creating an Android Application

    This tutorial describes developing Qt Quick applications for Android devices
    using Qt Quick Controls.

    This tutorial describes how to use \QC to implement a Qt Quick application
    that accelerates an SVG (Scalable Vector Graphics) image based on the
    changing accelerometer values.

    \image creator_android_tutorial_ex_app.png

    \section1 Creating the Project

    \list 1

        \li Select \gui File > \gui {New File or Project} > \gui Applications >
            \gui {Qt Quick Application} > \gui Choose.

        \li In the \gui{Name} field, type \b{accelbubble}.

        \li In the \gui {Create in} field, enter the path for the project files.
            For example, \c {C:\Qt\examples}, and then click \gui{Next} (or
            \gui Continue on Mac OS X).

        \li In the \gui {Qt Quick component set} field, select
            \gui {Qt Quick Controls 1.0}.

        \li Select an Android \l{glossary-buildandrun-kit}{kit} for ARM,
            and click \gui{Next}.

            \note Kits are listed if they have been specified in \gui Tools >
            \gui Options > \gui{Build & Run} > \gui Kits.

        \li Select \gui Next in the following dialogs to use the default
            settings.

        \li Review the project settings, and click \gui{Finish} (or \gui Done on
            Mac OS X).

    \endlist

    \QC generates a default QML file that you can modify to create the main view
    of the application.

    \section1 Creating the Main View

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

    To use the Bluebubble.svg used by the Qt Sensors example, Accel Bubble, in
    your project, you must copy it to the project directory (same subdirectory
    as the QML file) from the examples directory in the Qt installation
    directory. For example:
    \c {C:\Qt\Qt5.2.0\5.2.0\msvc2010\examples\sensors\accelbubble\content}.
    The image appears in the \gui Resources pane. You can also use any other
    image or a QML type, instead.

    \list 1

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

        \li Modify the properties of the ApplicationWindow type to specify the
            application name, give the ApplicationWindow an id, and to set it
            visible, as illustrated by the following code snippet:

            \quotefromfile accelbubble/main.qml
            \skipto ApplicationWindow
            \printuntil visible
            \skipto /^\}/
            \printuntil }

        \li Click \gui Design to open the file in \QMLD.

        \li In the \gui Navigator pane, select \gui Button and press \key Delete
            to delete it.

        \li In the \gui Library view, \gui Resources tab, select Bluebubble.svg
            and drag and drop it to the canvas.

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

        \li In the code editor, add the following new properties to the image to
            position the image at the center of ApplicationWindow when the
            application starts:

            \quotefromfile accelbubble/main.qml
            \skipto Image
            \printuntil bubble.width

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

            \dots
            \printuntil centerY
            \skipto /^\}/
            \printuntil }
    \endlist

    Here is how the accelbubble.qml file looks after you made the changes:

    \quotefromfile accelbubble/main.qml
    \skipto import QtQuick
    \printuntil 1.0
    \codeline
    \skipto ApplicationWindow
    \printuntil true

    \skipto Image
    \printuntil y:
    \skipto /^\}/
    \printuntil }


    \section1 Moving the Bubble

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

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

            \code
            import QtSensors 5.0
            \endcode

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

            \quotefromfile accelbubble/main.qml
            \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:

            \quotefromfile accelbubble/main.qml
            \skipto function
            \printuntil Math.atan(x
            \printuntil }

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

            \quotefromfile accelbubble/main.qml
            \skipto onReadingChanged
            \printuntil }

        \li Add SmoothedAnimation behavior on the \a x and \a y properties of
            the bubble to make its movement look smoother.

            \quotefromfile accelbubble/main.qml
            \skipto Behavior
            \printuntil x
            \printuntil }
            \printuntil }
     \endlist

    \section1 Adding Dependencies

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

    \code
    QT += quick sensors svg xml
    \endcode

    \section1 Running the Application

    The application is complete and ready to be deployed to an Android device:

    \list 1

        \li Enable \e{USB Debugging} on the device.

        \li 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, check
    "Always allow from the computer" and select \gui OK.

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

    \endlist

    \section1 Example Code

    When you have completed the steps, the main.qml file should look as follows:

    \quotefile accelbubble/main.qml

*/