There is built-in support for automatictypeconversion between QVariantList andJavaScript arrays,and QVariantMap andJavaScript objects.

For example, the function defined inQMLbelow left expects two arguments, an array and an object, and printstheircontents using the standard JavaScript syntax for array and object itemaccess.The C++ code below right calls this function, passing a QVariantList anda QVariantMap,whichare automatically converted to JavaScript array and object values,repectively:

Similarly, if a C++ type uses a QVariantList or QVariantMap typefor a property ormethod parameter, the value can be created as a JavaScriptarray or object in theQML side, and is automatically converted to a QVariantList or QVariantMap whenit is passed to C++.

Using enumerations of acustom type

To use an enumeration from a customC++component, the enumeration must be declared with Q_ENUMS() toregisterit with Qt's meta object system. For example, the following C++ typehas a Status enum:

1.class ImageViewer : public QDeclarativeItem

2. {

3.Q_OBJECT

4. Q_ENUMS(Status)

5. Q_PROPERTY(Status status READstatus NOTIFYstatusChanged)

6.public:

7.enum Status {

8. Ready,

9. Loading,

10. Error

11. };

12.

13. Status status() const;

14.signals:

15.void statusChanged();

16.};

Providing the ImageViewer class has been registeredusing qmlRegisterType(),its Status enum can now be used fromQML:

1.ImageViewer {

2. onStatusChanged: {

3.if (status == ImageViewer.Ready)

4. console.log("Image viewer isready!")

5. }

6.}

The C++ type must be registered with QMLtouse its enums. If your C++ type is not instantiable, it can be registeredusing qmlRegisterUncreatableType().Tobe accessible from QML, the names of enum values must begin with acapitalletter.

C++ signals may pass enumeration valuesassignal parameters to QML, providing that the enumeration and the signalaredeclared within the same class, or that the enumeration value is one ofthosedeclared in the Qt Namespace.

Additionally, if a C++ signal with anenumparameter should be connectable to a QML function using the connect()function,the enum type must be registered using qRegisterMetaType().

For QML signals, enum values may be usedassignal parameters using the int type:

1.ImageViewer {

2.signal someOtherSignal(int statusValue)

3.

4. Component.onCompleted: {

5. someOtherSignal(ImageViewer.Loading)

6. }

7.}

Automatic type conversionfrom strings

As a convenience, some basic types canbespecified in QML using format strings to make it easier to pass simplevaluesfrom QML to C++.

These string formats can be used to setQML property values and passargumentsto C++ functions. This is demonstrated by various examples on thispage; in theabove Qt properties example,theApplicationData class has a backgroundColor property of a QColor type,which is set fromthe QML code with the string "red" rather ratherthan an actual QColor object.

If it is preferred to pass anexplicitly-typedvalue rather than a string, the global Qt objectprovidesconveniencefunctions for creating some of the object types listed above. Forexample,Qt.rgba() createsa QColor valuefrom four RGBAvalues. The QColor returnedfrom thisfunction could be used instead of a string to set a QColor-typepropertyor to call a C++ function that requires aQColor parameter.

The Qt resource system allowsresource files tobe stored as binary files in an application executable. Thiscan be useful whenbuilding a mixed QML/C++ application as it enables QML files(as well as otherresources such as images and sound files) to be referred tothrough theresource system URI scheme rather than relative or absolute paths tofilesystemresources. Note, however, that if you use the resource system, theapplicationexecutable must be re-compiled whenever a QML source file is changedin orderto update the resources in the package.

·From C++, load the main QML file as aresource using the :/ prefix oras aURL with the qrcscheme

Once this is done, all files specifiedbyrelative paths in QML will be loaded from the resource system instead. Useofthe resource system is completely transparent to the QML layer; this meansallQML code should refer to resource files using relative paths andshould not use the qrc scheme. This schemeshould only beused from C++ code for referring to resource files.