9b4e52c2ef200993f33c6e912c6ea3116a7bd701
[profile/ivi/qtdeclarative.git] / doc / src / declarative / qtbinding.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 qtbinding.html
30 \inqmlmodule QtQuick 2
31 \ingroup qml-features
32 \previouspage {Extending QML Functionalities using C++}
33 \nextpage {Integrating QML Code with Existing Qt UI Code}
34 \contentspage QML Features
35 \title Using QML Bindings in C++ Applications
36
37 QML is designed to be easily extensible to and from C++. The classes in the
38 Qt Declarative module allow QML components to be loaded and manipulated from C++, and through
39 Qt's \l{The Meta-Object System}{meta-object system}, QML and C++ objects can easily
40 communicate through Qt signals and slots. In addition, QML plugins can be written to create
41 reusable QML components for distribution.
42
43 You may want to mix QML and C++ for a number of reasons. For example:
44
45 \list
46 \o To use functionality defined in a C++ source (for example, when using a C++ Qt-based data model, or
47 calling functions in a third-party C++ library)
48 \o To access functionality in the Qt Declarative module (for example, to dynamically generate
49 images using QDeclarativeImageProvider)
50 \o To write your own QML elements (whether for your applications, or for distribution to others)
51 \endlist
52
53 To use the Qt Declarative module, you must include and link to the module appropriately, as shown on
54 the \l {QtDeclarative}{module index page}. The \l {Qt Declarative UI Runtime} documentation
55 shows how to build a basic C++ application that uses this module.
56
57
58 \section1 Core module classes
59
60 The Qt Declarative module provides a set of C++ APIs for extending your QML applications from C++ and
61 embedding QML into C++ applications. There are several core classes in the Qt Declarative module
62 that provide the essential capabilities for doing this. These are:
63
64 \list
65 \o QDeclarativeEngine: A QML engine provides the environment for executing QML code. Every
66 application requires at least one engine instance.
67 \o QDeclarativeComponent: A component encapsulates a \l{QML Documents}{QML document}.
68 \o QDeclarativeContext: A context allows an application to expose data to the QML components
69 created by an engine.
70 \endlist
71
72 A QDeclarativeEngine allows the configuration of global settings that apply to all of its QML
73 component instances: for example, the QNetworkAccessManager to be used for network communications,
74 and the file path to be used for persistent storage.
75
76 QDeclarativeComponent is used to load QML documents. Each QDeclarativeComponent instance represents
77 a single document. A component can be created from the URL or file path of a QML document, or the raw
78 QML code of the document. Component instances are instatiated through the
79 QDeclarativeComponent::create() method, like this:
80
81 \code
82 QDeclarativeEngine engine;
83 QDeclarativeComponent component(&engine, QUrl::fromLocalFile("MyRectangle.qml"));
84 QObject *rectangleInstance = component.create();
85
86 // ...
87 delete rectangleInstance;
88 \endcode
89
90 QML documents can also be loaded using QDeclarativeView. This class provides a convenient
91 QWidget-based view for embedding QML components into QGraphicsView-based applications. (For other
92 methods of integrating QML into QWidget-based applications, see \l {Integrating QML Code with existing Qt
93 UI code}.)
94
95
96 \section1 Approaches to using QML with C++
97
98 There are a number of ways to extend your QML application through C++. For example, you could:
99
100 \list
101 \o Load a QML component and manipulate it (or its children) from C++
102 \o Embed a C++ object and its properties directly into a QML component (for example, to make a
103 particular C++ object callable from QML, or to replace a dummy list model with a real data set)
104 \o Define new QML elements (through QObject-based C++ classes) and create them directly from your
105 QML code
106 \endlist
107
108 These methods are shown below. Naturally these approaches are not exclusive; you can mix any of
109 these methods throughout your application as appropriate.
110
111
112 \section2 Loading QML Components from C++
113
114 A QML document can be loaded with QDeclarativeComponent or QDeclarativeView. QDeclarativeComponent
115 loads a QML component as a C++ object; QDeclarativeView also does this,
116 but additionally loads the QML component directly into a QGraphicsView. It is convenient for loading
117 a displayable QML component into a QWidget-based application.
118
119 For example, suppose there is a \c MyItem.qml file that looks like this:
120
121 \snippet doc/src/snippets/declarative/qtbinding/loading/MyItem.qml start
122 \snippet doc/src/snippets/declarative/qtbinding/loading/MyItem.qml end
123
124 This QML document can be loaded with QDeclarativeComponent or QDeclarativeView with the following
125 C++ code. Using a QDeclarativeComponent requires calling QDeclarativeComponent::create() to create
126 a new instance of the component, while a QDeclarativeView automatically creates an instance of the
127 component, which is accessible via QDeclarativeView::rootObject():
128
129 \table
130 \row
131 \o
132 \snippet doc/src/snippets/declarative/qtbinding/loading/main.cpp QDeclarativeComponent-a
133 \dots 0
134 \snippet doc/src/snippets/declarative/qtbinding/loading/main.cpp QDeclarativeComponent-b
135 \o
136 \snippet doc/src/snippets/declarative/qtbinding/loading/main.cpp QDeclarativeView
137 \endtable
138
139 This \c object is the instance of the \c MyItem.qml component that has been created. You can now
140 modify the item's properties using QObject::setProperty() or QDeclarativeProperty:
141
142 \snippet doc/src/snippets/declarative/qtbinding/loading/main.cpp properties
143
144 Alternatively, you can cast the object to its actual type and call functions with compile-time
145 safety. In this case the base object of \c MyItem.qml is an \l Item, which is defined by the
146 QDeclarativeItem class:
147
148 \snippet doc/src/snippets/declarative/qtbinding/loading/main.cpp cast
149
150 You can also connect to any signals or call functions defined in the component using
151 QMetaObject::invokeMethod() and QObject::connect(). See \l {Exchanging data between QML and C++}
152 below for further details.
153
154 \section3 Locating child objects
155
156 QML components are essentially object trees with children that have siblings and their own children.
157 Child objects of QML components can be located using the QObject::objectName property with
158 QObject::findChild(). For example, if the root item in \c MyItem.qml had a child \l Rectangle item:
159
160 \snippet doc/src/snippets/declarative/qtbinding/loading/MyItem.qml start
161 \codeline
162 \snippet doc/src/snippets/declarative/qtbinding/loading/MyItem.qml child
163 \snippet doc/src/snippets/declarative/qtbinding/loading/MyItem.qml end
164
165 The child could be located like this:
166
167 \snippet doc/src/snippets/declarative/qtbinding/loading/main.cpp findChild
168
169 If \c objectName is used inside a delegate of a ListView, \l Repeater or some other
170 element that creates multiple instances of its delegates, there will be multiple children with
171 the same \c objectName. In this case, QObject::findChildren() can be used to find all children
172 with a matching \c objectName.
173
174 \warning While it is possible to use C++ to access and manipulate QML objects deep into the
175 object tree, we recommend that you do not take this approach outside of application
176 testing and prototyping. One strength of QML and C++ integration is the ability to implement the
177 QML user interface separately from the C++ logic and dataset backend, and this strategy breaks if the
178 C++ side reaches deep into the QML components to manipulate them directly. This would make it difficult
179 to, for example, swap a QML view component for another view, if the new component was missing a
180 required \c objectName. It is better for the C++ implementation to know as little as possible about
181 the QML user interface implementation and the composition of the QML object tree.
182
183
184 \section2 Embedding C++ Objects into QML Components
185
186 When loading a QML scene into a C++ application, it can be useful to directly embed C++ data into
187 the QML object. QDeclarativeContext enables this by exposing data to the context of a QML
188 component, allowing data to be injected from C++ into QML.
189
190 For example, here is a QML item that refers to a \c currentDateTime value that does not exist in
191 the current scope:
192
193 \snippet doc/src/snippets/declarative/qtbinding/context/MyItem.qml 0
194
195 This \c currentDateTime value can be set directly by the C++ application that loads the QML
196 component, using QDeclarativeContext::setContextProperty():
197
198 \snippet doc/src/snippets/declarative/qtbinding/context/main.cpp 0
199
200 Context properties can hold either QVariant or QObject* values. This means custom C++ objects can
201 also be injected using this approach, and these objects can be modified and read directly in QML.
202 Here, we modify the above example to embed a QObject instance instead of a QDateTime value, and the QML code
203 invokes a method on the object instance:
204
205 \table
206 \row
207 \o
208 \snippet doc/src/snippets/declarative/qtbinding/context-advanced/applicationdata.h 0
209 \codeline
210 \snippet doc/src/snippets/declarative/qtbinding/context-advanced/main.cpp 0
211 \o
212 \snippet doc/src/snippets/declarative/qtbinding/context-advanced/MyItem.qml 0
213 \endtable
214
215 (Note that date/time values returned from C++ to QML can be formatted through
216 \l{QML:Qt::formatDateTime}{Qt.formatDateTime()} and associated functions.)
217
218 If the QML item needs to receive signals from the context property, it can connect to them using the
219 \l Connections element. For example, if \c ApplicationData has a signal named \c
220 dataChanged(), this signal can be connected to using an \c onDataChanged handler within
221 a \l Connections object:
222
223 \snippet doc/src/snippets/declarative/qtbinding/context-advanced/connections.qml 0
224
225 Context properties can be useful for using C++ based data models in a QML view. See the
226 \l {declarative/modelviews/stringlistmodel}{String ListModel},
227 \l {declarative/modelviews/objectlistmodel}{Object ListModel} and
228 \l {declarative/modelviews/abstractitemmodel}{AbstractItemModel} models for
229 respective examples on using QStringListModel, QObjectList-based models and QAbstractItemModel
230 in QML views.
231
232 Also see the QDeclarativeContext documentation for more information.
233
234
235 \section2 Defining New QML Elements
236
237 While new QML elements can be \l {Defining New Components}{defined in QML}, they can also be
238 defined by C++ classes; in fact, many of the core \l {QML Elements} are implemented through
239 C++ classes. When you create a QML object using one of these elements, you are simply creating an
240 instance of a QObject-based C++ class and setting its properties.
241
242 To create a visual item that fits in with the Qt Quick elements, base your class off \l QDeclarativeItem instead of QObject directly.
243 You can then implement your own painting and functionality like any other QGraphicsObject. Note that QGraphicsItem::ItemHasNoContents is set by default on QDeclarativeItem because
244 it does not paint anything; you will need to clear this if your item is supposed to paint anything (as opposed to being solely for input handling or logical grouping).
245
246 For example, here is an \c ImageViewer class with an \c image URL property:
247
248 \snippet doc/src/snippets/declarative/qtbinding/newelements/imageviewer.h 0
249
250 Aside from the fact that it inherits QDeclarativeItem, this is an ordinary class that could
251 exist outside of QML. However, once it is registered with the QML engine using qmlRegisterType():
252
253 \snippet doc/src/snippets/declarative/qtbinding/newelements/main.cpp register
254
255 Then, any QML code loaded by your C++ application or \l{QDeclarativeExtensionPlugin}{plugin} can create and manipulate
256 \c ImageViewer objects:
257
258 \snippet doc/src/snippets/declarative/qtbinding/newelements/standalone.qml 0
259
260
261 It is advised that you avoid using QGraphicsItem functionality beyond the properties documented in QDeclarativeItem.
262 This is because the GraphicsView backend is intended to be an implementation detail for QML, so the QtQuick items can be moved to faster backends as they become available with no change from a QML perspective.
263 To minimize any porting requirements for custom visual items, try to stick to the documented properties in QDeclarativeItem where possible. Properties QDeclarativeItem inherits but doesn't document are classed as implementation details; they are not officially supported and may disappear between releases.
264
265 Note that custom C++ types do not have to inherit from QDeclarativeItem; this is only necessary if it is
266 a displayable item. If the item is not displayable, it can simply inherit from QObject.
267
268 For more information on defining new QML elements, see the \l {Tutorial: Writing QML extensions with C++}
269 {Writing QML extensions with C++} tutorial and the
270 \l {Extending QML Functionalities using C++} reference documentation.
271
272
273
274 \section1 Exchanging Data between QML and C++
275
276 QML and C++ objects can communicate with one another through signals, slots and property
277 modifications. For a C++ object, any data that is exposed to Qt's \l{The Meta-Object System}{Meta-Object System}
278 - that is, properties, signals, slots and Q_INVOKABLE methods - become available to QML. On
279 the QML side, all QML object data is automatically made available to the meta-object system and can
280 be accessed from C++.
281
282
283 \section2 Calling Functions
284
285 QML functions can be called from C++ and vice-versa.
286
287 All QML functions are exposed to the meta-object system and can be called using
288 QMetaObject::invokeMethod(). Here is a C++ application that uses this to call a QML function:
289
290 \table
291 \row
292 \o \snippet doc/src/snippets/declarative/qtbinding/functions-qml/MyItem.qml 0
293 \o \snippet doc/src/snippets/declarative/qtbinding/functions-qml/main.cpp 0
294 \endtable
295
296 Notice the Q_RETURN_ARG() and Q_ARG() arguments for QMetaObject::invokeMethod() must be specified as
297 QVariant types, as this is the generic data type used for QML functions and return values.
298
299 To call a C++ function from QML, the function must be either a Qt slot, or a function marked with
300 the Q_INVOKABLE macro, to be available to QML. In the following example, the QML code invokes
301 methods on the \c myObject object, which has been set using QDeclarativeContext::setContextProperty():
302
303 \table
304 \row
305 \o
306 \snippet doc/src/snippets/declarative/qtbinding/functions-cpp/MyItem.qml 0
307 \o
308 \snippet doc/src/snippets/declarative/qtbinding/functions-cpp/myclass.h 0
309 \codeline
310 \snippet doc/src/snippets/declarative/qtbinding/functions-cpp/main.cpp 0
311 \endtable
312
313 QML supports the calling of overloaded C++ functions. If there are multiple C++ functions with the
314 same name but different arguments, the correct function will be called according to the number and
315 the types of arguments that are provided.
316
317
318 \section2 Receiving Signals
319
320 All QML signals are automatically available to C++, and can be connected to using QObject::connect()
321 like any ordinary Qt C++ signal. In return, any C++ signal can be received by a QML object using
322 \l {Signal Handlers}{signal handlers}.
323
324 Here is a QML component with a signal named \c qmlSignal. This signal is connected to a C++ object's
325 slot using QObject::connect(), so that the \c cppSlot() method is called whenever the \c qmlSignal
326 is emitted:
327
328 \table
329 \row
330 \o
331 \snippet doc/src/snippets/declarative/qtbinding/signals-qml/MyItem.qml 0
332 \o
333 \snippet doc/src/snippets/declarative/qtbinding/signals-qml/myclass.h 0
334 \codeline
335 \snippet doc/src/snippets/declarative/qtbinding/signals-qml/main.cpp 0
336 \endtable
337
338 To connect to Qt C++ signals from within QML, use a signal handler with the \c on<SignalName> syntax.
339 If the C++ object is directly creatable from within QML (see \l {Defining new QML elements} above)
340 then the signal handler can be defined within the object declaration. In the following example, the
341 QML code creates a \c ImageViewer object, and the \c imageChanged and \c loadingError signals of the
342 C++ object are connected to through \c onImagedChanged and \c onLoadingError signal handlers in QML:
343
344 \table
345 \row
346 \o
347
348 \snippet doc/src/snippets/declarative/qtbinding/signals-cpp/imageviewer.h start
349 \dots 4
350 \snippet doc/src/snippets/declarative/qtbinding/signals-cpp/imageviewer.h end
351
352 \o
353 \snippet doc/src/snippets/declarative/qtbinding/signals-cpp/standalone.qml 0
354 \endtable
355
356 (Note that if a signal has been declared as the NOTIFY signal for a property, QML allows it to be
357 received with an \c on<Property>Changed handler even if the signal's name does not follow the \c
358 <Property>Changed naming convention. In the above example, if the "imageChanged" signal was named
359 "imageModified" instead, the \c onImageChanged signal handler would still be called.)
360
361 If, however, the object with the signal is not created from within the QML code, and the QML item only has a
362 reference to the created object - for example, if the object was set using
363 QDeclarativeContext::setContextProperty() - then the \l Connections element can be used
364 instead to create the signal handler:
365
366 \table
367 \row
368 \o \snippet doc/src/snippets/declarative/qtbinding/signals-cpp/main.cpp connections
369 \o \snippet doc/src/snippets/declarative/qtbinding/signals-cpp/MyItem.qml 0
370 \endtable
371
372 C++ signals can use enum values as parameters provided that the enum is declared in the
373 class that is emitting the signal, and that the enum is registered using Q_ENUMS.
374 See \l {Using enumerations of a custom type} below for details.
375
376
377 \section2 Modifying Properties
378
379 Any properties declared in a QML object are automatically accessible from C++. Given a QML item
380 like this:
381
382 \snippet doc/src/snippets/declarative/qtbinding/properties-qml/MyItem.qml 0
383
384 The value of the \c someNumber property can be set and read using QDeclarativeProperty, or
385 QObject::setProperty() and QObject::property():
386
387 \snippet doc/src/snippets/declarative/qtbinding/properties-qml/main.cpp 0
388
389 You should always use QObject::setProperty(), QDeclarativeProperty or QMetaProperty::write() to
390 change a QML property value, to ensure the QML engine is made aware of the property change. For example,
391 say you have a custom element \c PushButton with a \c buttonText property that internally reflects
392 the value of a \c m_buttonText member variable. Modifying the member variable directly like this is
393 not a good idea:
394
395 \badcode
396 // BAD!
397 QDeclarativeComponent component(engine, "MyButton.qml");
398 PushButton *button = qobject_cast<PushButton*>(component.create());
399 button->m_buttonText = "Click me";
400 \endcode
401
402 Since the value is changed directly, this bypasses Qt's \l{The Meta-Object System}{meta-object system}
403 and the QML engine is not made aware of the property change. This means property bindings to
404 \c buttonText would not be updated, and any \c onButtonTextChanged handlers would not be called.
405
406
407 \target properties-cpp
408
409 Any \l {The Property System}{Qt properties} - that is, those declared with the Q_PROPERTY()
410 macro - are accessible from QML. Here is a modified version of the \l {Embedding C++ objects into
411 QML components}{earlier example} on this page; here, the \c ApplicationData class has a \c backgroundColor
412 property. This property can be written to and read from QML:
413
414 \table
415 \row
416 \o \snippet doc/src/snippets/declarative/qtbinding/properties-cpp/applicationdata.h 0
417 \o \snippet doc/src/snippets/declarative/qtbinding/properties-cpp/MyItem.qml 0
418 \endtable
419
420 Notice the \c backgroundColorChanged signal is declared as the NOTIFY signal for the
421 \c backgroundColor property. If a Qt property does not have an associated NOTIFY signal,
422 the property cannot be used for \l {Property Binding} in QML, as the QML engine would not be
423 notified when the value changes. If you are using custom types in QML, make sure their
424 properties have NOTIFY signals so that they can be used in property bindings.
425
426 See \l {Tutorial: Writing QML extensions with C++} for further details and examples
427 on using Qt properties with QML.
428
429
430 \section1 Supported data types
431
432 Any C++ data that is used from QML - whether as custom properties, or parameters for signals or
433 functions - must be of a type that is recognizable by QML.
434
435 By default, QML recognizes the following data types:
436
437 \list
438 \o bool
439 \o unsigned int, int
440 \o float, double, qreal
441 \o QString
442 \o QUrl
443 \o QColor
444 \o QDate, QTime, QDateTime
445 \o QPoint, QPointF
446 \o QSize, QSizeF
447 \o QRect, QRectF
448 \o QVariant
449 \o QVariantList, QVariantMap
450 \o QObject*
451 \o Enumerations declared with Q_ENUMS()
452 \endlist
453
454 To allow a custom C++ type to be created or used in QML, the C++ class must be registered as a QML
455 type using qmlRegisterType(), as shown in the \l {Defining new QML elements} section above.
456
457
458 \section2 JavaScript Arrays and Objects
459
460 There is built-in support for automatic type conversion between QVariantList and JavaScript
461 arrays, and QVariantMap and JavaScript objects.
462
463 For example, the function defined in QML below left expects two arguments, an array and an object, and prints
464 their contents using the standard JavaScript syntax for array and object item access. The C++ code
465 below right calls this function, passing a QVariantList and a QVariantMap, which are automatically
466 converted to JavaScript array and object values, repectively:
467
468 \table
469 \header
470 \o Type
471 \o String format
472 \o Example
473 \row
474 \o \snippet doc/src/snippets/declarative/qtbinding/variantlistmap/MyItem.qml 0
475 \o \snippet doc/src/snippets/declarative/qtbinding/variantlistmap/main.cpp 0
476 \endtable
477
478 This produces output like:
479
480 \code
481 Array item: 10
482 Array item: #00ff00
483 Array item: bottles
484 Object item: language = QML
485 Object item: released = Tue Sep 21 2010 00:00:00 GMT+1000 (EST)
486 \endcode
487
488 Similarly, if a C++ type uses a QVariantList or QVariantMap type for a property or method
489 parameter, the value can be created as a JavaScript array or object in the QML
490 side, and is automatically converted to a QVariantList or QVariantMap when it is passed to C++.
491
492
493 \section2 Using Enumerations of a Custom Type
494
495 To use an enumeration from a custom C++ component, the enumeration must be declared with Q_ENUMS() to
496 register it with Qt's meta object system. For example, the following C++ type has a \c Status enum:
497
498 \snippet doc/src/snippets/declarative/qtbinding/enums/imageviewer.h start
499 \snippet doc/src/snippets/declarative/qtbinding/enums/imageviewer.h end
500
501 Providing the \c ImageViewer class has been registered using qmlRegisterType(), its \c Status enum can
502 now be used from QML:
503
504 \snippet doc/src/snippets/declarative/qtbinding/enums/standalone.qml 0
505
506 The C++ type must be registered with QML to use its enums. If your C++ type is not instantiable, it
507 can be registered using qmlRegisterUncreatableType().  To be accessible from QML, the names of enum values
508 must begin with a capital letter.
509
510 See the \l {Tutorial: Writing QML extensions with C++}{Writing QML extensions with C++} tutorial and
511 the \l {Extending QML Functionalities using C++} reference documentation for
512 more information.
513
514
515 \section2 Using Enumeration Values as Signal and Method Parameters
516
517 C++ signals may pass enumeration values as signal parameters to QML, providing that the enumeration
518 and the signal are declared within the same class, or that the enumeration value is one of those declared
519 in the \l {Qt}{Qt Namespace}.
520
521 Likewise, invokable C++ method parameters may be enumeration values providing 
522 that the enumeration and the method are declared within the same class, or that
523 the enumeration value is one of those declared in the \l {Qt}{Qt Namespace}.
524
525 Additionally, if a C++ signal with an enum parameter should be connectable to a QML function using the
526 \l{QML Signal and Handler Event System#Connecting Signals to Methods and Signals}{connect()}
527 function, the enum type must be registered using qRegisterMetaType().
528
529 For QML signals, enum values may be used as signal parameters using the \c int type:
530
531 \snippet doc/src/snippets/declarative/qtbinding/enums/standalone.qml 1
532
533
534 \section2 Automatic Type Conversion from Strings
535
536 As a convenience, some basic types can be specified in QML using format strings to make it easier to
537 pass simple values from QML to C++.
538
539 \table
540 \header
541 \o Type
542 \o String format
543 \o Example
544 \row
545 \o QColor
546 \o Color name, "#RRGGBB", "#RRGGBBAA"
547 \o "red", "#ff0000", "#ff000000"
548 \row
549 \o QDate
550 \o "YYYY-MM-DD"
551 \o "2010-05-31"
552 \row
553 \o QPoint
554 \o "x,y"
555 \o "10,20"
556 \row
557 \o QRect
558 \o "x,y,WidthxHeight"
559 \o "50,50,100x100"
560 \row
561 \o QSize
562 \o "WidthxHeight"
563 \o "100x200"
564 \row
565 \o QTime
566 \o "hh:mm:ss"
567 \o "14:22:55"
568 \row
569 \o QUrl
570 \o URL string
571 \o "http://www.example.com"
572 \row
573 \o QVector3D
574 \o "x,y,z"
575 \o "0,1,0"
576 \row
577 \o Enumeration value
578 \o Enum value name
579 \o "AlignRight"
580 \endtable
581
582 (More details on these string formats and types can be found in the
583 \l {QML Basic Types}{basic type documentation}.)
584
585 These string formats can be used to set QML \c property values and pass arguments to C++
586 functions. This is demonstrated by various examples on this page; in the above
587 \l{#properties-cpp}{Qt properties example}, the \c ApplicationData class has a \c backgroundColor
588 property of a QColor type, which is set from the QML code with the string "red" rather rather
589 than an actual QColor object.
590
591 If it is preferred to pass an explicitly-typed value rather than a string, the global
592 \l{QmlGlobalQtObject}{Qt object} provides convenience functions for creating some of the object
593 types listed above. For example, \l{QML:Qt::rgba()}{Qt.rgba()} creates a QColor value from four
594 RGBA values. The QColor returned from this function could be used instead of a string to set
595 a QColor-type property or to call a C++ function that requires a QColor parameter.
596
597
598 \section1 Writing QML plugins
599
600 The Qt Declarative module includes the QDeclarativeExtensionPlugin class, which is an abstract
601 class for writing QML plugins. This allows QML extension types to be dynamically loaded into
602 QML applications.
603
604 See the QDeclarativeExtensionPlugin documentation and \l {How to Create Qt Plugins} for more
605 details.
606
607
608 \section1 Managing resource files with the Qt resource system
609
610 The \l {The Qt Resource System}{Qt resource system} allows resource files to be stored as
611 binary files in an application executable. This can be useful when building a mixed
612 QML/C++ application as it enables QML files (as well as other resources such as images
613 and sound files) to be referred to through the resource system URI scheme rather than
614 relative or absolute paths to filesystem resources. Note, however, that if you use the resource
615 system, the application executable must be re-compiled whenever a QML source file is changed
616 in order to update the resources in the package.
617
618 To use the resource system in a mixed QML/C++ application:
619
620 \list
621 \o Create a \c .qrc \l {The Qt Resource System}{resource collection file} that lists resource
622    files in XML format
623 \o From C++, load the main QML file as a resource using the \c :/ prefix or as a URL with the
624    \c qrc scheme
625 \endlist
626
627 Once this is done, all files specified by relative paths in QML will be loaded from
628 the resource system instead. Use of the resource system is completely transparent to
629 the QML layer; this means all QML code should refer to resource files using relative
630 paths and should \i not use the \c qrc scheme. This scheme should only be used from
631 C++ code for referring to resource files.
632
633 Here is a application packaged using the \l {The Qt Resource System}{Qt resource system}.
634 The directory structure looks like this:
635
636 \code
637 project
638     |- example.qrc
639     |- main.qml
640     |- images
641         |- background.png
642     |- main.cpp
643     |- project.pro
644 \endcode
645
646 The \c main.qml and \c background.png files will be packaged as resource files. This is
647 done in the \c example.qrc resource collection file:
648
649 \quotefile doc/src/snippets/declarative/qtbinding/resources/example.qrc
650
651 Since \c background.png is a resource file, \c main.qml can refer to it using the relative
652 path specified in \c example.qrc:
653
654 \snippet doc/src/snippets/declarative/qtbinding/resources/main.qml 0
655
656 To allow QML to locate resource files correctly, the \c main.cpp loads the main QML
657 file, \c main.qml, as a resource file using the \c qrc scheme:
658
659 \snippet doc/src/snippets/declarative/qtbinding/resources/main.cpp 0
660
661 Finally \c project.pro uses the RESOURCES variable to indicate that \c example.qrc should
662 be used to build the application resources:
663
664 \quotefile doc/src/snippets/declarative/qtbinding/resources/resources.pro
665
666 See \l {The Qt Resource System} for more information.
667
668 */
669
670