2013-06-11 14:23:35 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** 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
|
|
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\li Select \gui File > \gui {New File or Project} > \gui Applications >
|
|
|
|
|
\gui {Qt Quick 2 Application (Qt Quick Controls)} > \gui Choose.
|
2013-06-11 14:23:35 +02:00
|
|
|
|
|
|
|
|
\li In the \gui{Name} field, type \b{accelbubble}.
|
|
|
|
|
|
|
|
|
|
\li In the \gui {Create in} field, enter the path for the project files.
|
2013-10-28 11:10:55 +01:00
|
|
|
For example, \c {C:\Qt\examples}, and then click \gui{Next} (or
|
|
|
|
|
\gui Continue on Mac OS X).
|
2013-06-11 14:23:35 +02:00
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\li Select an Android \l{glossary-buildandrun-kit}{kit} for ARM,
|
2013-06-11 14:23:35 +02:00
|
|
|
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.
|
|
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\li Review the project settings, and click \gui{Finish} (or \gui Done on
|
|
|
|
|
Mac OS X).
|
|
|
|
|
|
2013-06-11 14:23:35 +02:00
|
|
|
\endlist
|
|
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\QC generates a default QML file that you can modify to create the main view
|
|
|
|
|
of the application.
|
2013-06-11 14:23:35 +02:00
|
|
|
|
|
|
|
|
\section1 Creating the Main View
|
|
|
|
|
|
|
|
|
|
The main view of the application displays an SVG bubble image at the center
|
|
|
|
|
of the main window.
|
|
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
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.
|
2013-06-11 14:23:35 +02:00
|
|
|
|
|
|
|
|
\list 1
|
|
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\li In the \gui Projects view, double-click the main.qml file
|
|
|
|
|
to open it in the code editor.
|
2013-06-11 14:23:35 +02:00
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\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:
|
2013-06-11 14:23:35 +02:00
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\quotefromfile accelbubble/main.qml
|
2013-06-11 14:23:35 +02:00
|
|
|
\skipto ApplicationWindow
|
2013-10-28 11:10:55 +01:00
|
|
|
\printuntil visible
|
2013-06-11 14:23:35 +02:00
|
|
|
\skipto /^\}/
|
|
|
|
|
\printuntil }
|
|
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\li Click \gui Design to open the file in \QMLD.
|
2013-06-11 14:23:35 +02:00
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\li In the \gui Navigator pane, select \gui Button and press \key Delete
|
|
|
|
|
to delete it.
|
2013-06-11 14:23:35 +02:00
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\li In the \gui Library view, \gui Resources tab, select Bluebubble.svg
|
|
|
|
|
and drag and drop it to the canvas.
|
2013-06-11 14:23:35 +02:00
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\li In the \gui Properties pane, \gui Id field, enter \e bubble to be
|
|
|
|
|
able to reference the image from other places.
|
2013-06-11 14:23:35 +02:00
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\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:
|
2013-06-11 14:23:35 +02:00
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\quotefromfile accelbubble/main.qml
|
2013-06-11 14:23:35 +02:00
|
|
|
\skipto Image
|
2013-10-28 11:10:55 +01:00
|
|
|
\printuntil bubble.width
|
2013-06-11 14:23:35 +02:00
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\li Set the x and y position of the image based on the new
|
|
|
|
|
properties:
|
2013-06-11 14:23:35 +02:00
|
|
|
|
|
|
|
|
\dots
|
2013-10-28 11:10:55 +01:00
|
|
|
\printuntil centerY
|
|
|
|
|
\skipto /^\}/
|
2013-06-11 14:23:35 +02:00
|
|
|
\printuntil }
|
|
|
|
|
\endlist
|
|
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
Here is how the accelbubble.qml file looks after you made the changes:
|
2013-06-11 14:23:35 +02:00
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\quotefromfile accelbubble/main.qml
|
2013-06-11 14:23:35 +02:00
|
|
|
\skipto import QtQuick
|
|
|
|
|
\printuntil 1.0
|
|
|
|
|
\codeline
|
|
|
|
|
\skipto ApplicationWindow
|
|
|
|
|
\printuntil true
|
|
|
|
|
|
|
|
|
|
\skipto Image
|
2013-10-28 11:10:55 +01:00
|
|
|
\printuntil y:
|
|
|
|
|
\skipto /^\}/
|
|
|
|
|
\printuntil }
|
2013-06-11 14:23:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
\section1 Moving the Bubble
|
|
|
|
|
|
|
|
|
|
Now that the visual elements are in place, let us move the bubble based on
|
|
|
|
|
Accelerometer sensor values.
|
|
|
|
|
|
|
|
|
|
\list 1
|
2013-10-28 11:10:55 +01:00
|
|
|
\li Add the following import statement to main.qml:
|
2013-06-11 14:23:35 +02:00
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\code
|
|
|
|
|
import QtSensors 5.0
|
|
|
|
|
\endcode
|
2013-06-11 14:23:35 +02:00
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\li Add the Accelerometer type with the necessary properties:
|
2013-06-11 14:23:35 +02:00
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\quotefromfile accelbubble/main.qml
|
2013-06-11 14:23:35 +02:00
|
|
|
\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:
|
|
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\quotefromfile accelbubble/main.qml
|
|
|
|
|
\skipto function
|
|
|
|
|
\printuntil Math.atan(x
|
|
|
|
|
\printuntil }
|
2013-06-11 14:23:35 +02:00
|
|
|
|
|
|
|
|
\li Add the following JavaScript code for \a onReadingChanged signal of
|
|
|
|
|
Accelerometer type to make the bubble move when the Accelerometer
|
|
|
|
|
values change:
|
|
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\quotefromfile accelbubble/main.qml
|
|
|
|
|
\skipto onReadingChanged
|
|
|
|
|
\printuntil }
|
2013-06-11 14:23:35 +02:00
|
|
|
|
|
|
|
|
\li Add SmoothedAnimation behavior on the \a x and \a y properties of
|
|
|
|
|
the bubble to make its movement look smoother.
|
|
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\quotefromfile accelbubble/main.qml
|
|
|
|
|
\skipto Behavior
|
|
|
|
|
\printuntil x
|
|
|
|
|
\printuntil }
|
|
|
|
|
\printuntil }
|
2013-06-11 14:23:35 +02:00
|
|
|
\endlist
|
|
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\section1 Adding Dependencies
|
2013-06-11 14:23:35 +02:00
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
Update the accelbubble.pro file with the following library dependency
|
|
|
|
|
information:
|
2013-06-11 14:23:35 +02:00
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\code
|
|
|
|
|
QT += quick sensors svg xml
|
|
|
|
|
\endcode
|
2013-06-11 14:23:35 +02:00
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\section1 Running the Application
|
2013-06-11 14:23:35 +02:00
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
The application is complete and ready to be deployed to an Android device:
|
2013-06-11 14:23:35 +02:00
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\list 1
|
2013-06-11 14:23:35 +02:00
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\li Enable \e{USB Debugging} on the device.
|
2013-06-11 14:23:35 +02:00
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\li Connect the device to the development PC.
|
2013-06-11 14:23:35 +02:00
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\li To run the application on the device, press \key {Ctrl+R}.
|
2013-06-11 14:23:35 +02:00
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\endlist
|
2013-06-11 14:23:35 +02:00
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\section1 Example Code
|
2013-06-11 14:23:35 +02:00
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
When you have completed the steps, the main.qml file should look as follows:
|
2013-06-11 14:23:35 +02:00
|
|
|
|
2013-10-28 11:10:55 +01:00
|
|
|
\quotefile accelbubble/main.qml
|
2013-06-11 14:23:35 +02:00
|
|
|
|
|
|
|
|
*/
|