236d92d1bf525cf335a520438515f00299205c5c
[profile/ivi/qtdeclarative.git] / doc / src / declarative / qtprogrammers.qdoc
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the documentation of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:FDL$
10 ** GNU Free Documentation License
11 ** Alternatively, this file may be used under the terms of the GNU Free
12 ** Documentation License version 1.3 as published by the Free Software
13 ** Foundation and appearing in the file included in the packaging of
14 ** this file.
15 **
16 ** Other Usage
17 ** Alternatively, this file may be used in accordance with the terms
18 ** and conditions contained in a signed written agreement between you
19 ** and Nokia.
20 **
21 **
22 **
23 **
24 ** $QT_END_LICENSE$
25 **
26 ****************************************************************************/
27
28 /*!
29 \page qtprogrammers.html
30 \inqmlmodule QtQuick 2
31 \target qtprogrammers
32 \title QML for Qt Programmers
33
34 While QML does not require Qt knowledge to use, if you \i are already familiar with Qt,
35 much of your knowledge is directly relevant to learning and using QML. Of course,
36 an application with a UI defined in QML also uses Qt for all the non-UI logic.
37
38 \section1 Familiar Concepts
39
40 QML provides direct access to the following concepts from Qt:
41
42 \list
43  \o QAction - the \l {QML Basic Types}{action} type
44  \o QObject signals and slots - available as functions to call in JavaScript
45  \o QObject properties - available as variables in JavaScript
46  \o QWidget - QDeclarativeView is a QML-displaying widget
47  \o Qt models - used directly in data binding (QAbstractItemModel)
48 \endlist
49
50 Qt knowledge is \i required for \l {Extending QML Functionalities using C++},
51 and also for \l{Integrating QML Code with existing Qt UI code}.
52
53 \section1 QML Items compared with QWidgets
54
55 QML Items are very similar to QWidgets: they define the look and feel of the user interface. (Note that while QWidgets
56 haven't traditionally been used to define the look and feel of view delegates, QML Items can be used for this as well.)
57
58 There are three structurally different types of QWidget:
59
60 \list
61  \o Simple widgets that are not used as parents (QLabel, QCheckBox, QToolButton, etc.)
62  \o Parent widgets that are normally used as parents to other widgets (QGroupBox, QStackedWidget, QTabWidget, etc.)
63  \o Compound widgets that are internally composed of child widgets (QComboBox, QSpinBox, QFileDialog, QTabWidget, etc.)
64 \endlist
65
66 QML Items also serve these purposes. Each is considered separately below.
67
68 \section2 Simple Widgets
69
70 The most important rule to remember while implementing a new QDeclarativeItem in C++
71 is that it should not contain any look and feel policies - leave that to the
72 QML usage of the item.
73
74 As an example, imagine you wanted a reusable Button item. If you therefore
75 decided to write a QDeclarativeItem subclass to implement a button,
76 just as QToolButton subclasses QWidget for this purpose, following the rule above, your
77 \c QDeclarativeButton would not have any appearance - just the notions of enabled, triggering, etc.
78
79 But there is already an object in Qt that does this: QAction.
80
81 QAction is the UI-agnostic essence of QPushButton, QCheckBox, QMenu items, QToolButton,
82 and other visual widgets that are commonly bound to a QAction.
83
84 So, the job of implementing a checkbox abstraction for QML is already done - it's QAction.
85 The look and feel of an action - the appearance of the button, the transition between states,
86 and exactly how it respond to mouse, key, or touch input, should all be left for definition
87 in QML.
88
89 It is illustrative to note that QDeclarativeTextEdit is built upon QTextControl,
90 QDeclarativeWebView is built upon QWebPage, and ListView uses QAbstractItemModel,
91 just as QTextEdit, QWebView, and QListView are built upon
92 those same UI-agnostic components.
93
94 The encapsulation of the look and feel that QWidgets gives is important, and for this
95 the QML concept of \l {qdeclarativedocuments.html}{components} serves the same purpose. If you are building a complete
96 suite of applications which should have a consistent look and feel, you should build
97 a set of reusable components with the look and feel you desire.
98
99 So, to implement your reusable button, you would simply build a QML component.
100
101
102 \section2 Parent Widgets
103
104 Parent widgets each provide a generic way to interface to one or more arbitrary other widgets.
105 A QTabWidget provides an interface to multiple "pages", one of which is visible at any time,
106 and a mechanism for selecting among them (the QTabBar). A QScrollArea provides scrollbars around
107 a widget that is otherwise too large to fit in available space.
108
109 Nearly all such components can be created directly in QML. Only a few cases
110 which require very particular event handling, such as Flickable, require C++ implementations.
111
112 As an example, imagine you decided to make a generic tab widget item to be used
113 through your application suite wherever information is in such quantity that it
114 needs to be divided up into pages.
115
116 A significant difference in the parenting concept with QML compare to QWidgets
117 is that while child items are positioned relative to their parents,
118 there is no requirement that they be wholly contained ("clipped") to
119 the parent (although the clipped property of the child Item does allow
120 this where it is needed).
121 This difference has rather far-reaching consequences, for example:
122
123 \list
124     \o A shadow or highlight around a widget could be a child of that widget.
125     \o Particle effects can flow outside the object where they originate.
126     \o Transitioning animations can "hide" items by visibly moving them beyond the screen bounds.
127 \endlist
128
129
130 \section2 Compound Widgets
131
132 Some widgets provide functionality by composing other widgets as an "implementation detail",
133 providing a higher level API to the composition. QSpinBox for example is a line edit and some
134 buttons to increase/decrease the edited value. QFileDialog uses a whole host of widgets to
135 give the user a way of finding and selecting a file name.
136
137 When developing reusable QML Items, you may choose to do the same: build an item composed
138 of other items you have already defined.
139
140 The only caveat when doing this is to consider the possible animations and transitions that
141 users of the compound item might wish to employ. For example, a spinbox might need to smoothly
142 transition from an arbitrary Text item, or characters within a Text item, so your spinbox
143 item would need to be sufficiently flexible to allow such animation.
144
145 \section1 QML Items Compared With QGraphicsWidgets
146
147 The main difference between QML items and QGraphicsWidgets is how they are intended to be used. The technical implementation details are much the same, but in practice they are different because QML items are made for declarative and compositional use, and QGraphicsWidgets are made for imperative and more integrated use. Both QML items and QGraphicsWidgets inherit from QGraphicsObject, and can co-exist. The differences are in the layouting system and the interfacing with other components. Note that, as QGraphicsWidgets tend more to be all-in-one packages, the equivalent of a QGraphicsWidget may be many QML items composed across several QML files, but it can still be loaded and used as a single QGraphicsObject from C++.
148
149 QGraphicsWidgets are usually designed to be laid out with QGraphicsLayouts. QML does not use QGraphicsLayouts, as the Qt layouts do not mix well with animated and fluid UIs, so the geometry interface is one of the main differences. When writing QML elements, you allow the designers to place their bounding rectangle using absolute geometry, bindings or anchors (all setup for you when you inherit QDeclarativeItem) and you do not use layouts or size hints. If size hints are appropriate, then place them in the QML documentation so that the designers know how to use the item best, but still have complete control over the look and feel.
150
151 The other main difference is that QGraphicsWidgets tend to follow the widget model, in that they are a self-contained bundle of UI and logic. In contrast, QML primitives are usually a single purpose item that does not fulfill a use case on its own, but is composed into the equivalent of the widget inside the QML file. So when writing QML Items, try to avoid doing UI logic or composing visual elements inside the items. Try instead to write more general purpose primitives, so that the look and feel (which involves the UI logic) can be written in QML.
152
153 Both differences are caused by the different method of interaction. QGraphicsWidget is a QGraphicsObject subclass which makes fluid UI development from C++ easier, and QDeclarativeItem is a QGraphicsObject subclass which makes fluid UI development from QML easier. The difference therefore is primarily one of the interface exposed, and the design of the items that come with it (the Declarative primitives for QML and the nothing for QGraphicsWidget, because you need to write your own UI logic into the subclass).
154
155 If you wish to use both QML and C++ to write the UI, for example to ease the transition period, it is recommended to use QDeclarativeItem subclasses (although you can use QGraphicsWidgets as well). To allow for easier use from C++ make the root item of each C++ component a LayoutItem, and load individual 'widgets' of QML (possibly comprised of multiple files, and containing a self-contained bundle of UI and logic) into your scene to replace individual QGraphicsWidgets one at a time.
156 */