summaryrefslogtreecommitdiffstats
path: root/doc/src
diff options
context:
space:
mode:
authorAaron Kennedy <aaron.kennedy@nokia.com>2009-04-30 03:09:22 (GMT)
committerAaron Kennedy <aaron.kennedy@nokia.com>2009-04-30 03:09:22 (GMT)
commit0282ea19722c247157c652ef9122379f0e715497 (patch)
tree9773641d150774bd8e66e87e33e5e8899a6a4a9c /doc/src
parentd85c0c07b72476d801db3f1cb622cb32ab50dcc4 (diff)
parente3c91e87a06b73a06c86f93c69951768874bbaf6 (diff)
downloadQt-0282ea19722c247157c652ef9122379f0e715497.zip
Qt-0282ea19722c247157c652ef9122379f0e715497.tar.gz
Qt-0282ea19722c247157c652ef9122379f0e715497.tar.bz2
Merge branch 'kinetic-declarativeui' of git@scm.dev.nokia.troll.no:qt/kinetic into kinetic-declarativeui
Conflicts: src/declarative/qml/parser/javascriptgrammar.cpp src/declarative/qml/parser/javascriptgrammar_p.h
Diffstat (limited to 'doc/src')
-rw-r--r--doc/src/declarative/elements.qdoc2
-rw-r--r--doc/src/tutorials/declarative.qdoc485
2 files changed, 329 insertions, 158 deletions
diff --git a/doc/src/declarative/elements.qdoc b/doc/src/declarative/elements.qdoc
index 8fff472..8955587 100644
--- a/doc/src/declarative/elements.qdoc
+++ b/doc/src/declarative/elements.qdoc
@@ -16,7 +16,7 @@ The following table lists the Qml elements provided by the Qt Declarative module
\o
\list
-\o \l StateGroup
+\o \l State
\o \l SetProperty
\o \l SetProperties
\o \l ParentChange
diff --git a/doc/src/tutorials/declarative.qdoc b/doc/src/tutorials/declarative.qdoc
index be8fad9..3724b10 100644
--- a/doc/src/tutorials/declarative.qdoc
+++ b/doc/src/tutorials/declarative.qdoc
@@ -80,9 +80,9 @@
This means you should use the duiviewer application provided with
Qt to run the examples.
- \list 1
+ \list
\o \l{tutorials/declarative/contacts/part1}{Drawing and Animation}
- \o \l{tutorials/declarative/contacts/part2}{Reuse of QML components}
+ \o \l{tutorials/declarative/contacts/part2}{Reusing QML Components}
\o \l{tutorials/declarative/contacts/part3}{Models, Views and Delegates}
\o \l{tutorials/declarative/contacts/part4}{Other Tricks}
\endlist
@@ -121,27 +121,14 @@
\image declarative-roundrect.png
- \code
- <Rect id="removeButton"
- width="30" height="30"
- color="red"
- radius="5"/>
- \endcode
+ \snippet declarative/tutorials/contacts/1_Drawing_and_Animation/1/RemoveButton.qml 0
This is the simplest of QML components. It describes a rectangle with
some simple properties. In QML all components start with a capital
letter, and their properties with lower case letters. Properties
can either be declared as XML attributes or as children of the
- component element. The above rectangle could equally be written
+ component element.
- \code
- <Rect id="removeButton" color="red">
- <width>30</width>
- <height>30</height>
- <radius>5</radius>
- </Rect>
- \endcode
-
The rectangle component is one of the more simple QML components. Apart
from the properties all QML components share, it has the properties
@@ -183,18 +170,7 @@
\image declarative-removebutton-close.png
- \code
- <Rect id="removeButton"
- width="30" height="30"
- color="red"
- radius="5">
- <Image id="trashIcon"
- width="22" height="22"
- anchors.right="{parent.right}" anchors.rightMargin="4"
- anchors.verticalCenter="{parent.verticalCenter}"
- src="../shared/pics/trash.png"/>
- </Rect>
- \endcode
+ \snippet declarative/tutorials/contacts/1_Drawing_and_Animation/2/RemoveButton.qml 0
The trashIcon image is added as a child of the Rectangle. In this case
the <children> tag isn't used because the default property of the
@@ -202,20 +178,7 @@
and use some other default component, when this is the case its possible
to explicitly list the sub component as a child as follows:
- \code
- <Rect id="removeButton"
- width="30" height="30"
- color="red"
- radius="5">
- <children>
- <Image id="trashIcon"
- width="22" height="22"
- anchors.right="{parent.right}" anchors.rightMargin="4"
- anchors.verticalCenter="{parent.verticalCenter}"
- src="../shared/pics/trash.png"/>
- </children>
- </Rect>
- \endcode
+ \snippet declarative/tutorials/contacts/1_Drawing_and_Animation/2a/RemoveButton.qml 0
The Image element allows loading an image file for display. The source
specified is a URL, and in this case refers to a portable network graphics
@@ -252,31 +215,7 @@
This is a wider rectangle with two images and some text. The code to
draw this state of the button could be written as follows:
- \code
- <Rect id="removeButton"
- width="230" height="30"
- color="red"
- radius="5">
- <Image id="cancelIcon"
- width="22" height="22"
- anchors.right="{parent.right}" anchors.rightMargin="4"
- anchors.verticalCenter="{parent.verticalCenter}"
- src="../shared/pics/cancel.png"/>
- <Image id="confirmIcon"
- width="22" height="22"
- anchors.left="{parent.left}" anchors.leftMargin="4"
- anchors.verticalCenter="{parent.verticalCenter}"
- src="../shared/pics/ok.png"/>
- <Text id="text"
- anchors.verticalCenter="{parent.verticalCenter}"
- anchors.left="{confirmIcon.right}" anchors.leftMargin="4"
- anchors.right="{cancelIcon.left}" anchors.rightMargin="4"
- font.bold="true"
- color="white"
- hAlign="AlignHCenter"
- text="Remove"/>
- </Rect>
- \endcode
+ \snippet declarative/tutorials/contacts/1_Drawing_and_Animation/3/RemoveButton.qml 0
The rectangle with is now wider by 200 pixels. Also the trashIcon has
been replaced with the confirm state children. Normally we wouldn't
@@ -310,21 +249,32 @@
might look like.
\code
- <Rect id="removeButton"
- width="30" height="30"
- color="red"
- radius="5">
- <Image id="trashIcon"
- width="22" height="22"
- anchors.right="{parent.right}" anchors.rightMargin="4"
- anchors.verticalCenter="{parent.verticalCenter}"
- src="../shared/pics/trash.png"/>
- <Image id="cancelIcon"
- width="22" height="22"
- anchors.right="{parent.right}" anchors.rightMargin="4"
- anchors.verticalCenter="{parent.verticalCenter}"
- src="../shared/pics/cancel.png"
- opacity="0"/>
+ Rect {
+ id: removeButton
+ width: 30
+ height: 30
+ color: "red"
+ radius: 5
+ Image {
+ id: trashIcon
+ width: 22
+ height: 22
+ anchors.right: parent.right
+ anchors.rightMargin: 4
+ anchors.verticalCenter: parent.verticalCenter
+ src: "../../shared/pics/trash.png"
+ opacity: 1
+ }
+ Image {
+ id: cancelIcon
+ width: 22
+ height: 22
+ anchors.right: parent.right
+ anchors.rightMargin: 4
+ anchors.verticalCenter: parent.verticalCenter
+ src: "../../shared/pics/cancel.png"
+ opacity: 0
+ }
\endcode
The code above includes components from both states of the RemoveButton,
@@ -335,17 +285,7 @@
should be changed. For the RemoveButton there is only one non-base state
required. In this tutorial we will name it the 'opened' state.
- \code
- <states>
- <State name="opened">
- <SetProperty target="{removeButton}" property="width" value="230"/>
- <SetProperty target="{text}" property="opacity" value="1"/>
- <SetProperty target="{confirmIcon}" property="opacity" value="1"/>
- <SetProperty target="{cancelIcon}" property="opacity" value="1"/>
- <SetProperty target="{trashIcon}" property="opacity" value="0"/>
- </State>
- </states>
- \endcode
+ \snippet declarative/tutorials/contacts/1_Drawing_and_Animation/4/RemoveButton.qml states
In the opened state the width of the button itself changes from the base
width of 30 to the new width of 230. Also the opacity of the children
@@ -357,18 +297,7 @@
To trigger the change we will react to the 'clicked' signal of a
MouseRegion component.
- \code
- <Image id="trashIcon"
- width="22" height="22"
- anchors.right="{parent.right}" anchors.rightMargin="4"
- anchors.verticalCenter="{parent.verticalCenter}"
- src="../shared/pics/trash.png"
- opacity="1">
- <MouseRegion
- anchors.fill="{parent}"
- onClicked="toggle()"/>
- </Image>
- \endcode
+ \snippet declarative/tutorials/contacts/1_Drawing_and_Animation/4/RemoveButton.qml mouse region
MouseRegion components handle mouse actions within their geometry. This
geometry behaves the same way as painted components, such that children
@@ -381,7 +310,7 @@
a function called toggle() is called. It might also have been written
\code
- onClicked="removeButton.state='opened'"
+ onClicked: { removeButton.state='opened' }
\endcode
However in this case we are using a function because it allows multiple
@@ -391,69 +320,24 @@
The toggle() function is a new function specified as part of the remove
button element.
- \code
- <resources>
- <Script>
- function toggle() {
- print('removeButton.toggle()');
- if (removeButton.state == 'opened') {
- removeButton.state = '';
- } else {
- removeButton.state = 'opened';
- }
- }
- </Script>
- </resources>
- \endcode
+ \snippet declarative/tutorials/contacts/1_Drawing_and_Animation/4/RemoveButton.qml script
Any QML component can have a set of resources specified. One of those
resources is any Script that might be needed. See the
{QtScript Module}{QtScript Module} for more information on how to write
- script code in Qt. There are only a couple of additional items to
- note when using Script with QML components. The first is that it
- is an xml file, that means either CDATA or other forms of escaping
- should be used if special characters are needed. For example,
- the expression;
-
- \code
- if (a && b) {}
- \endcode
-
- Should either be escaped as:
-
- \code
- if (a &amp;&amp; b) {}
- \endcode
+ script code in Qt.
- or enclosed in a CDATA section as
-
- \code
- <![CDATA[if (a && b) {}]]>
- \endcode
-
- The other item to note is that you can refer to identified QML components
+ It is possible to refer to identified QML components
within the script. Hence the function for our RemoveButton will check
if the state is already open to determine what the new state should be.
- We also have added a print function. This isn't required for the button
- to function, but is useful for tracking down possible bugs when
- working with QML.
-
- See the file RemoveButton4.qml for the full multi-state specification.
-
\section1 Animation
Currently the RemoveButton is function, but snaps between our two states.
Fortunately making the transition between states smooth is very simple.
We only need one more bit of code at the end of our removeButton component.
- \code
- <transitions>
- <Transition fromState="*" toState="opened" reversible="true">
- <NumericAnimation properties="opacity,x,width" duration="200"/>
- </Transition>
- </transitions>
- \endcode
+ \snippet declarative/tutorials/contacts/1_Drawing_and_Animation/5/RemoveButton.qml transition
All QML components have a transitions property. This describes how
properties of items within the component should change. In this case
@@ -462,9 +346,296 @@
to complete their transition.
\omit
- TODO More on types of animation
+ TODO More on types of animation, e.g. ColorAnimation, Behaviors.
\endomit
In the next chapter we will show how we can use the remove button in
other QML components.
*/
+
+/*!
+ \page tutorials-declarative-contacts-part2.html
+ \contentspage {Declarative UI Tutorial}{Contents}
+ \previouspage {tutorials/declarative/contacts/part1}{Chapter 1}
+ \nextpage {tutorials/declarative/contacts/part3}{Chapter 3}
+ \example tutorials/declarative/contacts/part2
+ \title Reusing QML Components
+ \tableofcontents
+
+ The second part of this tutorial covers how to reuse QML components and
+ have them interact with each other. The RemoveButton developed in the
+ previous chapter is intended to be part of a more complex control for
+ editing a field of our contact. This ContactField in turn is intended
+ to be used in a contact editing control.
+
+ \section1 Loading QML Components
+
+ Reusing the RemoveButton itself is very simple. When parsing a QML file
+ if a Component is referred to that isn't already in the system, Qt
+ will try to load it from a file of the same name with the ".qml" extension.
+
+ \snippet declarative/tutorials/contacts/2_Reuse/1/ContactField.qml load
+
+ The above QML code will attempt to load the RemoveButton component from
+ a file with the name "RemoveButton.qml" from the following search paths.
+
+ \list
+ \o Any imported directories. These are listed at the start of the file using
+ \c { import "path" }.
+ \o The run directory
+ \o The run directory + "/qml"
+ \o the directory of the QML code file
+ \o the directory of the QML code file + "/qml"
+ \endlist.
+
+ All the properties of the button are
+ accessible and can be overridden from defaults. The loaded component
+ can also refer to elements further up in the tree, so that code within
+ RemoveButton.qml could refer to the contactField component. However only
+ properties of the top level element in RemoveButton.qml are visible to
+ the contact field. In order to allow contact field to modify how wide
+ the remove button will be when opened we need to add a property to the
+ remove button.
+
+ \section1 Properties and Signals
+
+ \snippet declarative/tutorials/contacts/2_Reuse/2/RemoveButton.qml define properties and signals
+
+ These properties and signals are accessed from the contact field the same
+ way standard system components are accessed.
+
+ \snippet declarative/tutorials/contacts/2_Reuse/2/RemoveButton.qml use properties and signals
+
+ Now when the remove button is expanded, it will expand to the width of the
+ contact field. Also when the user confirms the remove action, the
+ text section of the contact field will be cleared. When creating a
+ component that does have children out of its own
+ bounds its important to consider whether the item should be clipped,
+ which is done above with \c{clip: true}.
+
+ \section1 States
+
+ Its also possible to access the state of included components. The FieldText
+ component we will use in this tutorial is also been written specifically
+ for our contacts application, as was the RemoveButton component. In
+ this case we want it to expand when editing. One way to do this would
+ be to anchor the field text component to the center of its parent and
+ then let its own width change push the remove button away, however that
+ would make it difficult to have the remove button also push the field
+ text to the left when the remove button expands.
+
+ So instead we will anchor the right edge of the field text to
+ the left edge of the remove button and use a state change in the
+ contact field itself to move the remove button and the field icon out of
+ view.
+
+ \snippet declarative/tutorials/contacts/2_Reuse/3/RemoveButton.qml all
+
+ Apart from accessing the fieldText.state, the above code also uses the when
+ attribute of its own editingText state. This is an alternative to using
+ a signal to change state. When the value of the expression for the
+ when attribute changes, Qt will detect if the contactField needs to enter
+ that state. In the FieldText element a similar approach is used to fade
+ out the label of the FieldText when the user enters some text of their own.
+
+ \snippet declarative/tutorials/contacts/3_Reuse/2/FieldText.qml behavior
+
+ fieldText is the enclosing component and textEdit is a TextEdit element
+ provided by Qt. In the QML code above, the opacity of the textLabel is
+ only 1 if there is text for the textEdit is empty. This is a form of
+ short cut to using states for an element, useful if only one property
+ is changing as it is for the textLabel. To animate a property change is
+ similar to animating a state change. Using the Behavior element we can
+ specify how the property changes if it does change state, allowing for
+ a smooth transition.
+
+ The fieldText element also handles changes to the text using the
+ onValueChanged attribute when specifying properties.
+
+ \snippet declarative/tutorials/contacts/2_Reuse/3/FieldText.qml value change
+
+ Because the user needs to be able to edit text in the text edit, it
+ shouldn't be simply bound to the text property of the FieldText component.
+ However if a component using the FieldText component sets the text
+ property of the FieldText component it should in turn set the text
+ of the text edit.
+
+ \section1 Key and Mouse Focus
+
+ Unlike in Qt setting focus to true on a component does not always mean
+ that the component has focus. This is due to the declarative nature
+ of QML, and can be affected by multiple components both indicating
+ focus to be true. At the time of writing this tutorial both key and mouse
+ focus handling are still being improved. Hence we will only lightly cover
+ the topic.
+
+ Normally in QML this is handled by FocusRealm components. A focus realm
+ is a sort of cut off point for determining focus. If a FocusRealm does
+ not have focus then any children of it won't be able to get focus even
+ if they do set focus to true. If your component has multiple child
+ components that could gain focus ensure that they are guarded by FocusRealm
+ component, and add code to handle which focus realms have focus
+ at that level. The alternative and approach done at this stage in
+ the tutorial is to only have one component set focus to true at a time.
+
+ Currently if multiple contact fields were put into our contact editor,
+ any of the FieldText components could be clicked and opened, and
+ any of the RemoveButton components could be clicked and opened, all
+ at the same time. We would like this behavior to be some what modal
+ instead, encouraging the user to either accept or cancel the current
+ action before moving onto a new action.
+
+ In the tutorial we do this with a property of our top level component
+ to handle whether we are in this state or not.
+
+ \snippet declarative/tutorials/contacts/2_Reuse/4/Contact.qml grab property
+
+ And in the code where we want to check or avoid allowing mouse interaction.
+
+ \snippet declarative/tutorials/contacts/2_Reuse/4/RemoveButton.qml grab
+
+ Handling Key and Mouse focus in QML is quite likely to change before
+ the Qt 4.6 release.
+*/
+
+/*!
+ \page tutorials-declarative-contacts-part3.html
+ \contentspage {Declarative UI Tutorial}{Contents}
+ \previouspage {tutorials/declarative/contacts/part2}{Chapter 2}
+ \nextpage {tutorials/declarative/contacts/part4}{Chapter 4}
+ \example tutorials/declarative/contacts/part3
+ \title Models, Views and Delegates
+ \tableofcontents
+
+ In the previous chapters we designed a component to display and
+ edit a contact. The next step is to display a list of those contacts
+ and allow the user to expand individual contacts for editing.
+
+ As the previous elements will not be changed in this section, they have
+ been moved to a lib directory for this tutorial and the relevant
+ name space path has been used.
+
+ \section1 Simple List View
+
+ Displaying lists requires three components. A model that holds the
+ data displayed, a delegate to indicate how elements are drawn and
+ a view to arrange the elements.
+
+ For the purposes of this tutorial we will be using an SQL query as our
+ data model. This can be declared in the resources section of
+ the parent item.
+
+ \snippet declarative/tutorials/contacts/3_Collections/1/ContactView.qml model
+
+ The SqlConnection component describes how to connect to the database in
+ much the same ways as the QSqlDatabase::addDatabase() function is used.
+ In this case an SQLite database is used as it can be connected to as a
+ file, reducing complexity in setting up a database server or credentials.
+
+ The SqlQuery component allows various forms of queries to be described.
+ When the query is a select statement, the component also acts as a model
+ allowing it to provide data to a ListView component. The query above
+ retrieves the fields recid, label, email and phone from a contacts table,
+ and orders the results by the label of the contact first, and then by
+ the recid for any contacts with equivalent labels.
+
+ The ListView component is suitable for displaying models, and is declared
+ much like any other QML component. However since it might have any number
+ of child items in the list, it has a property that defines how to construct
+ components for items when displayed.
+
+ \snippet declarative/tutorials/contacts/3_Collections/1/ContactView.qml delegate
+
+ Unlike a child element, this describes a template on how to build the component
+ for each element, much in the same way that components are loaded from
+ files such as RemoveButton.qml.
+
+ The entire view component will look like:
+
+ \snippet declarative/tutorials/contacts/3_Collections/1/ContactView.qml view
+
+ This gives us a list of contacts that the user can flick through.
+
+ .image.
+
+ \section1 Animating Delegates
+
+ The next step is to allow the user to click on a contact to edit the
+ contact. We will take advantage of QML to open a Contact component
+ in the list rather than as a new dialog or view. This is very
+ similar to how the contents of the FieldText and RemoveButton components
+ are swapped in and out.
+
+ \snippet declarative/tutorials/contacts/3_Collections/2/ContactView.qml components
+
+ The first step is to have two children of our delegate component that can
+ be swapped between. The plain Text component and the Contact component built
+ in the previous chapters. We also add a MouseRegion that can be clicked upon
+ to change the state of the delegate component.
+
+ \snippet declarative/tutorials/contacts/3_Collections/2/ContactView.qml states
+
+ This defines the open state of the delegate. It changes the height of the delegate
+ component to that of the whole list view, pushing the other items off each end of
+ the list. It sets the lists views scroll yPosition of the ListView to the
+ y value of the delegate so that the top of the delegate matches the top of the list view.
+ The next step is to lock the list view. This prevents the user being able to flick
+ the list view, meaning while in this state the delegate will continue to
+ fill the ListView's visible area. The final to properties that are set should
+ be familiar from previous chapters, setting the opacity of the items such
+ that the new item is visible and the old item hidden.
+
+ We then add a transition so that this becomes animated:
+
+ \snippet declarative/tutorials/contacts/3_Collections/2/ContactView.qml transition
+
+ This allows the user to click on an item to enter the open state.
+
+ .image.
+
+ Elsewhere on our contact view we add a button so that the user can leave the
+ detailed view of the contact.
+ \snippet declarative/tutorials/contacts/3_Collections/2/ContactView.qml button
+
+ And connect it's clicked value to some script to set the state of the delegate
+ back to its default state.
+
+ \snippet declarative/tutorials/contacts/3_Collections/2/ContactView.qml connection
+
+ Something worth noting at this point is that every delegate created has this connection.
+ It is important to check whether the delegate is the one in the open state, and
+ taking some effort to ensure only one is, before acting on the signal from the button.
+
+ \section1 Performance Considerations
+
+ We have now made a contact application that can view a list of contacts, open one,
+ and close it again. Its now time to take a moment and consider the implications
+ of a list view delegate. It is created for each and every item in the list,
+ and while the list cleans up after itself and only has delegate components constructed
+ for visible items and any single point of animation, the list can scroll very quickly.
+ This means potentially thousands of delegate components will be constructed and
+ destroyed when the user is flipping through the list.
+
+ Its important then to try and minimize the complexity of the delegate. This
+ can be done by delaying the loading of the component. By using the qml property
+ of the Item component, we can delay building the Contact.qml item until the user
+ attempts to open the list.
+
+ \snippet declarative/tutorials/contacts/3_Collections/3/ContactView.qml setting qml
+
+ Each item has a qml property that represents the filename for the contents of
+ a special qmlItem child of the Item. By setting the qml property of the Details
+ component on clicking the mouse region, the more complex component isn't loaded
+ until needed. The down side about this though is the properties of Contact
+ cannot be set until the item is loaded. This requires using the Bind
+ properties of an item.
+
+ \snippet declarative/tutorials/contacts/3_Collections/3/ContactView.qml binding
+
+ The Bind properties bind a value to another component, however the target of
+ this binding can be changed, unlike when setting the properties of a component
+ directly. This means that when the qml property is set, it will change the
+ qmlItem property of the Details component. This in turn triggers the Bind
+ elements to set the required properties of the qmlItem, which is now
+ an instance of the Contact component.
+*/