2011-09-27 11:56:42 +02:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (c) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2011-09-27 11:56:42 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator
|
2011-09-27 11:56:42 +02:00
|
|
|
**
|
|
|
|
**
|
|
|
|
** 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
|
2011-11-29 14:13:59 +01:00
|
|
|
\previouspage creator-tutorials.html
|
2011-09-27 11:56:42 +02:00
|
|
|
\page creator-qml-application.html
|
2012-08-23 16:10:58 +02:00
|
|
|
\nextpage creator-writing-program.html
|
2011-09-27 11:56:42 +02:00
|
|
|
|
2013-03-06 15:48:13 +01:00
|
|
|
\title Creating a Qt Quick Application
|
2011-09-27 11:56:42 +02:00
|
|
|
|
2013-03-13 11:39:36 +01:00
|
|
|
This tutorial uses built-in QML types and illustrates basic concepts of
|
2013-11-25 17:37:19 +01:00
|
|
|
\l{Qt Quick}.
|
2011-09-27 11:56:42 +02:00
|
|
|
|
2013-03-06 15:48:13 +01:00
|
|
|
This tutorial describes how to use \QC to implement Qt states and transitions. We use
|
2013-11-25 17:37:19 +01:00
|
|
|
\l{Animation}{Qt example code} to
|
2013-03-06 15:48:13 +01:00
|
|
|
create an application that displays a Qt logo that moves between three rectangles on the
|
|
|
|
page when you click them.
|
2011-09-27 11:56:42 +02:00
|
|
|
|
|
|
|
\image qmldesigner-tutorial.png "States and transitions example"
|
|
|
|
|
|
|
|
For more information about using \QMLD, see
|
|
|
|
\l{Developing Qt Quick Applications}.
|
|
|
|
|
|
|
|
\section1 Creating the Project
|
|
|
|
|
|
|
|
\list 1
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Select \gui{File > New File or Project > Applications >
|
2013-11-05 12:55:47 +01:00
|
|
|
Qt Quick Application > Choose}.
|
2011-09-27 11:56:42 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui{Name} field, type \b {Transitions}.
|
2011-11-15 09:48:08 +01:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui {Create in} field, enter the path for the project files.
|
2012-10-05 17:29:00 +02:00
|
|
|
For example, \c {C:\Qt\examples}, and then click \gui{Next} (on
|
|
|
|
Windows and Linux) or \gui Continue (on Mac OS).
|
2011-11-15 09:48:08 +01:00
|
|
|
|
2013-11-05 12:55:47 +01:00
|
|
|
\li In the \gui {Qt Quick component set} field, select
|
|
|
|
\gui {Qt Quick 2.0}.
|
|
|
|
|
|
|
|
\note The QML types used in this example are also supported in
|
|
|
|
Qt Quick 1.1. To create this example application for platforms that
|
|
|
|
run Qt 4, select \gui {Qt Quick 1.1}.
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Select \l{glossary-buildandrun-kit}{kits} for running and building your project,
|
2012-09-03 17:50:37 +02:00
|
|
|
and then click \gui{Next}.
|
2011-11-15 09:48:08 +01:00
|
|
|
|
2012-09-03 17:50:37 +02:00
|
|
|
\note Kits are listed if they have been specified in \gui Tools >
|
|
|
|
\gui Options > \gui {Build & Run} > \gui Kits.
|
2011-11-15 09:48:08 +01:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Select \gui Next in the following dialogs to use the default
|
2011-11-15 09:48:08 +01:00
|
|
|
settings.
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Review the project settings, and click \gui{Finish} (on Windows and
|
2012-10-05 17:29:00 +02:00
|
|
|
Linux) or \gui Done (on Mac OS) to create the project.
|
2011-09-27 11:56:42 +02:00
|
|
|
|
2013-06-13 17:36:37 +02:00
|
|
|
\li Press \key {Ctrl+R} (or \key {Cmd+R}) to run the application.
|
2011-09-27 11:56:42 +02:00
|
|
|
|
|
|
|
\endlist
|
|
|
|
|
|
|
|
\QC generates a default QML file that you can modify to create the main view
|
|
|
|
of the application.
|
|
|
|
|
|
|
|
\image qmldesigner-tutorial-project.png "Transitions project in Edit mode"
|
|
|
|
|
|
|
|
\section1 Creating the Main View
|
|
|
|
|
|
|
|
The main view of the application displays a Qt logo in the top left corner
|
|
|
|
of the screen and two empty rectangles.
|
|
|
|
|
|
|
|
To use the states.png image in your application, 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:
|
2013-03-06 15:48:13 +01:00
|
|
|
\c {C:\Qt\Qt5.0.1\5.0.1\msvc2010\examples\declarative\animation\states}. The image appears
|
2011-09-27 11:56:42 +02:00
|
|
|
in the \gui Resources pane. You can also use any other image or a QML
|
2013-03-13 11:39:36 +01:00
|
|
|
type, instead.
|
2011-09-27 11:56:42 +02:00
|
|
|
|
|
|
|
\list 1
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui Projects view, double-click the main.qml file
|
2011-09-27 11:56:42 +02:00
|
|
|
to open it in the code editor.
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Click \gui Design to open the file in \QMLD.
|
2011-09-27 11:56:42 +02:00
|
|
|
|
|
|
|
\image qmldesigner-tutorial-desing-mode.png "Transitions project in Design Mode"
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui Navigator pane, select \gui Text and press \key Delete to
|
2011-09-27 11:56:42 +02:00
|
|
|
delete it.
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Select \gui Rectangle to edit its properties.
|
2011-09-27 11:56:42 +02:00
|
|
|
|
|
|
|
\image qmldesigner-tutorial-page.png "Page properties"
|
|
|
|
|
|
|
|
\list a
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui Id field, enter \e page, to be able to reference the
|
2011-09-27 11:56:42 +02:00
|
|
|
rectangle from other places.
|
|
|
|
|
2013-11-06 17:25:38 +01:00
|
|
|
\li In the \gui Color field, set the color to #343434.
|
|
|
|
|
|
|
|
\li In the code editor, delete the \c {Qt.quit();} command.
|
2011-09-27 11:56:42 +02:00
|
|
|
|
|
|
|
\endlist
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui Library view, \gui Resources tab, select states.png and
|
2011-09-27 11:56:42 +02:00
|
|
|
drag and drop it to the canvas.
|
|
|
|
|
|
|
|
\image qmldesigner-tutorial-user-icon.png "Image properties"
|
|
|
|
|
|
|
|
\list a
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui Id field, enter \e icon.
|
2011-09-27 11:56:42 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui Position field, set \gui X to 10 and \gui Y to 20.
|
2011-09-27 11:56:42 +02:00
|
|
|
|
|
|
|
\endlist
|
|
|
|
|
2013-03-13 11:39:36 +01:00
|
|
|
\li In the \gui Library view, \gui {QML Types} tab, select \gui Rectangle,
|
2011-09-27 11:56:42 +02:00
|
|
|
drag and drop it to the canvas, and edit its properties.
|
|
|
|
|
|
|
|
\image qmldesigner-tutorial-topleftrect.png "Rectangle properties"
|
|
|
|
|
|
|
|
\list a
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui Id field, enter \e topLeftRect.
|
2011-09-27 11:56:42 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui Size field, set \gui W and \gui H to 64, for the
|
2011-09-27 11:56:42 +02:00
|
|
|
rectangle size to match the image size.
|
|
|
|
|
2013-11-06 17:25:38 +01:00
|
|
|
\li In the \gui Color field, click the
|
2011-09-27 11:56:42 +02:00
|
|
|
\inlineimage qmldesigner-transparent-button.png
|
2012-03-08 16:07:16 +01:00
|
|
|
(\gui Transparent) button to make the rectangle transparent.
|
2011-09-27 11:56:42 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui Border field, set the border color to #808080.
|
2011-09-27 11:56:42 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui Rectangle group, \gui Border field, set the border
|
2011-09-27 11:56:42 +02:00
|
|
|
width to 1.
|
|
|
|
|
|
|
|
\note If the \gui Border field does not appear after you set the
|
|
|
|
border color, try setting the border color to solid by clicking
|
|
|
|
the
|
|
|
|
\inlineimage qmldesigner-solid-color-button.png
|
2012-03-08 16:07:16 +01:00
|
|
|
(\gui {Solid Color}) button.
|
2011-09-27 11:56:42 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui Radius field, select 6 to create rounded corners for
|
2011-09-27 11:56:42 +02:00
|
|
|
the rectangle.
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Click \gui {Layout}, and then click the top and left anchor
|
2011-09-27 11:56:42 +02:00
|
|
|
buttons to anchor the rectangle to the top left corner of the
|
|
|
|
page.
|
|
|
|
|
|
|
|
\image qmldesigner-tutorial-topleftrect-layout.png "Layout tab"
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui Margin field, select 20 for the top anchor and 10
|
2011-09-27 11:56:42 +02:00
|
|
|
for the left anchor.
|
|
|
|
|
|
|
|
\endlist
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui Navigator pane, drag and drop the \gui {Mouse Area}
|
2013-03-13 11:39:36 +01:00
|
|
|
from \e page to \e topLeftRect to make it apply only to the
|
2011-09-27 11:56:42 +02:00
|
|
|
rectangle and not to the whole page.
|
|
|
|
|
2013-03-13 11:39:36 +01:00
|
|
|
\li Edit the \gui {Mouse Area} properties:
|
2011-09-27 11:56:42 +02:00
|
|
|
|
|
|
|
\list a
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Click \gui {Layout}, and then click the
|
2011-09-27 11:56:42 +02:00
|
|
|
\inlineimage qmldesigner-anchor-fill-screen.png
|
2012-03-08 16:07:16 +01:00
|
|
|
(\gui {Fill to Parent}) button to anchor the mouse area to the
|
|
|
|
rectangle.
|
2011-09-27 11:56:42 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the code editor, edit the pointer to the clicked expression
|
2013-03-13 11:39:36 +01:00
|
|
|
in the mouse area, as illustrated by the following code
|
2011-09-27 11:56:42 +02:00
|
|
|
snippet:
|
|
|
|
|
|
|
|
\qml
|
|
|
|
MouseArea {
|
|
|
|
anchors.fill: parent
|
|
|
|
onClicked: page.state = ''
|
|
|
|
}
|
|
|
|
\endqml
|
|
|
|
|
|
|
|
The expression sets the state to the base state and returns the
|
|
|
|
image to its initial position.
|
|
|
|
|
|
|
|
\endlist
|
|
|
|
|
2011-11-16 10:32:37 +01:00
|
|
|
The qml.main file should now look as follows:
|
|
|
|
|
2013-03-07 13:56:07 +01:00
|
|
|
\snippet transitions/main.qml 0
|
2011-11-16 10:32:37 +01:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui Navigator pane, copy topLeftRect (by pressing
|
2011-09-27 11:56:42 +02:00
|
|
|
\key {Ctrl+C}) and paste it to the canvas twice (by pressing
|
2013-03-13 11:39:36 +01:00
|
|
|
\key {Ctrl+V}). \QC renames the new instances of the type
|
2011-09-27 11:56:42 +02:00
|
|
|
topLeftRect1 and topLeftRect2.
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Select topLeftRect1 and edit its properties:
|
2011-09-27 11:56:42 +02:00
|
|
|
|
|
|
|
\list a
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui Id field, enter \e middleRightRect.
|
2011-09-27 11:56:42 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In \gui {Layout}, select the vertical center anchor button and
|
2011-09-27 11:56:42 +02:00
|
|
|
then the right anchor button to
|
|
|
|
anchor the rectangle to the middle right margin of the screen.
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui Margin field, select 10 for the right anchor and 0
|
2011-09-27 11:56:42 +02:00
|
|
|
for the vertical center anchor.
|
|
|
|
|
2013-06-13 17:36:37 +02:00
|
|
|
\li In the code editor, add a pointer to a clicked expression to the
|
2013-03-13 11:39:36 +01:00
|
|
|
mouse area. The following expression sets the state to
|
2011-09-27 11:56:42 +02:00
|
|
|
\e State1:
|
|
|
|
|
|
|
|
\c {onClicked: page.state = 'State1'}
|
|
|
|
|
|
|
|
You will create State1 later.
|
|
|
|
|
|
|
|
\endlist
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Select topLeftRect2 and edit its properties:
|
2011-09-27 11:56:42 +02:00
|
|
|
|
|
|
|
\list a
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui Id field, enter \e bottomLeftRect.
|
2011-09-27 11:56:42 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In \gui {Layout}, select the bottom and left anchor buttons to
|
2011-09-27 11:56:42 +02:00
|
|
|
anchor the rectangle to the bottom left margin of the screen.
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui Margin field, select 20 for the bottom anchor and 10
|
2011-09-27 11:56:42 +02:00
|
|
|
for the left anchor.
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the code editor, add a pointer to a clicked expression to the
|
2013-03-13 11:39:36 +01:00
|
|
|
mouse area. The following expression sets the state to
|
2011-09-27 11:56:42 +02:00
|
|
|
\e State2:
|
|
|
|
|
|
|
|
\c {onClicked: page.state = 'State2'}
|
|
|
|
|
|
|
|
You will create State2 later.
|
|
|
|
|
2011-11-16 10:32:37 +01:00
|
|
|
The qml.main file should now look as follows:
|
|
|
|
|
2013-03-07 13:56:07 +01:00
|
|
|
\snippet transitions/main.qml 1
|
2011-11-16 10:32:37 +01:00
|
|
|
|
2011-09-27 11:56:42 +02:00
|
|
|
\endlist
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Press \key {Ctrl+S} to save the changes.
|
2011-09-27 11:56:42 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Press \key {Ctrl+R} to run the application.
|
2011-09-27 11:56:42 +02:00
|
|
|
|
|
|
|
\endlist
|
|
|
|
|
|
|
|
\image qmldesigner-tutorial.png "States and transitions example"
|
|
|
|
|
|
|
|
You should see the Qt logo in the top left rectangle, and two additional
|
|
|
|
rectangles in the center right and bottom left of the screen.
|
|
|
|
|
|
|
|
You can now create additional states to add views to the application.
|
|
|
|
|
|
|
|
\section1 Adding Views
|
|
|
|
|
|
|
|
In the .qml file, you already created pointers to two additional states:
|
|
|
|
State1 and State2. To create the states:
|
|
|
|
|
|
|
|
\list 1
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Click the empty slot in the \gui States pane to create State1.
|
2011-09-27 11:56:42 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Click the empty slot in the \gui States pane to create State2.
|
2011-09-27 11:56:42 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the code editor, bind the position of the Qt logo to the
|
2011-09-27 11:56:42 +02:00
|
|
|
rectangle to make sure that the logo is displayed within the
|
|
|
|
rectangle when the view is scaled on different sizes of screens. Set
|
|
|
|
expressions for the x and y properties, as illustrated by the
|
|
|
|
following code snippet:
|
|
|
|
|
2013-01-23 12:37:22 +01:00
|
|
|
\snippet qml/states-properties.qml states
|
2011-09-27 11:56:42 +02:00
|
|
|
|
|
|
|
\image qmldesigner-tutorial-state1.png "States"
|
|
|
|
|
|
|
|
\note When you set the expressions, drag and drop is disabled for
|
|
|
|
the icon in \QMLD.
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Press \key {Ctrl+R} to run the application.
|
2011-09-27 11:56:42 +02:00
|
|
|
|
|
|
|
\endlist
|
|
|
|
|
|
|
|
Click the rectangles to move the Qt logo from one rectangle to another.
|
|
|
|
|
|
|
|
\section1 Adding Animation to the View
|
|
|
|
|
|
|
|
Add transitions to define how the properties change when the Qt logo moves
|
|
|
|
between states. The transitions apply animations to the Qt logo. For example,
|
|
|
|
the Qt logo bounces back when it moves to the middleRightRect and eases into
|
|
|
|
bottomLeftRect. Add the transitions in the code editor.
|
|
|
|
|
|
|
|
\list 1
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the code editor, add the following code to specify that when
|
2011-09-27 11:56:42 +02:00
|
|
|
moving to State1, the x and y coordinates of the Qt logo change
|
|
|
|
linearly over a duration of 1 second:
|
|
|
|
|
2013-01-23 12:37:22 +01:00
|
|
|
\snippet qml/list-of-transitions.qml first transition
|
2011-09-27 11:56:42 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li You can use the Qt Quick toolbar for animation to change the easing
|
2011-09-27 11:56:42 +02:00
|
|
|
curve type from linear to OutBounce:
|
|
|
|
|
|
|
|
\list a
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Click \gui NumberAnimation in the code editor to display the
|
2011-09-27 11:56:42 +02:00
|
|
|
\inlineimage qml-toolbar-indicator.png
|
|
|
|
icon, and then click the icon to open the toolbar:
|
|
|
|
|
|
|
|
\image qmldesigner-tutorial-quick-toolbar.png "Qt Quick toolbar for animation"
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui Easing field, select \gui Bounce.
|
2011-09-27 11:56:42 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui Subtype field, select \gui Out.
|
2011-09-27 11:56:42 +02:00
|
|
|
|
|
|
|
\endlist
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Add the following code to specify that when moving to State2, the x
|
2011-09-27 11:56:42 +02:00
|
|
|
and y coordinates of the Qt logo change over a duration of 2
|
|
|
|
seconds, and an InOutQuad easing function is used:
|
|
|
|
|
2013-01-23 12:37:22 +01:00
|
|
|
\snippet qml/list-of-transitions.qml second transition
|
2011-09-27 11:56:42 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Add the following code to specify that for any other state changes,
|
2011-09-27 11:56:42 +02:00
|
|
|
the x and y coordinates of the Qt logo change linearly over a
|
|
|
|
duration of 200 milliseconds:
|
|
|
|
|
2013-01-23 12:37:22 +01:00
|
|
|
\snippet qml/list-of-transitions.qml default transition
|
2011-09-27 11:56:42 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Press \key {Ctrl+R} to run the application.
|
2011-09-27 11:56:42 +02:00
|
|
|
|
|
|
|
\endlist
|
|
|
|
|
|
|
|
Click the rectangles to view the animated transitions.
|
|
|
|
|
2011-11-16 10:32:37 +01:00
|
|
|
\section1 Example Code
|
|
|
|
|
|
|
|
When you have completed the steps, the main.qml file should look as follows:
|
|
|
|
|
2013-03-07 13:56:07 +01:00
|
|
|
\snippet transitions/main.qml 2
|
2011-11-16 10:32:37 +01:00
|
|
|
|
2011-09-27 11:56:42 +02:00
|
|
|
*/
|