1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the documentation of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:FDL$
9 ** GNU Free Documentation License
10 ** Alternatively, this file may be used under the terms of the GNU Free
11 ** Documentation License version 1.3 as published by the Free Software
12 ** Foundation and appearing in the file included in the packaging of
16 ** Alternatively, this file may be used in accordance with the terms
17 ** and conditions contained in a signed written agreement between you
26 ****************************************************************************/
28 \page qtqml-cppclasses-component.html
29 \title Qt QML Module C++ Classes - QQmlComponent
30 \brief Description of QQmlComponent
32 \section2 Loading QML Components from C++
34 A QML document can be loaded with QQmlComponent or QQuickView.
35 QQmlComponent loads a QML component as a C++ QObject;
36 QQuickView also does this, but additionally loads the QML component
37 directly into a QQuickWindow which displays visual QML object types
38 provided by Qt Quick, or object types derived from those.
39 It is convenient for loading a displayable
40 QML component as a root QWindow.
42 For example, suppose there is a \c MyItem.qml file that looks like this:
44 \snippet qml/qtbinding/loading/MyItem.qml start
45 \snippet qml/qtbinding/loading/MyItem.qml end
47 This QML document can be loaded with QQmlComponent or
48 QQuickView with the following C++ code. Using a QQmlComponent
49 requires calling QQmlComponent::create() to create a new instance of
50 the component, while a QQuickView automatically creates an instance of
51 the component, which is accessible via QQuickView::rootObject():
56 \snippet qml/qtbinding/loading/main.cpp QQmlComponent-a
58 \snippet qml/qtbinding/loading/main.cpp QQmlComponent-b
60 \snippet qml/qtbinding/loading/main.cpp QQuickView
63 This \c object is the instance of the \c MyItem.qml component that has been
64 created. You can now modify the item's properties using
65 QObject::setProperty() or QQmlProperty:
67 \snippet qml/qtbinding/loading/main.cpp properties
69 Alternatively, you can cast the object to its actual type and call functions
70 with compile-time safety. In this case the base object of \c MyItem.qml is
71 an \l Item, which is defined by the QQuickItem class:
73 \snippet qml/qtbinding/loading/main.cpp cast
75 You can also connect to any signals or call functions defined in the
76 component using QMetaObject::invokeMethod() and QObject::connect(). See \l
77 {Interacting with Objects defined in QML from C++} for further details.