aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/doc/snippets/qml/qtbinding/loading/main.cpp
blob: f8c19a6b84b8941d5d43a281725a38e4aa5c2c6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include <QGuiApplication>
#include <QtQml>
#include <QtQuick>

static void withComponent()
{
//![QQmlComponent-a]
// Using QQmlComponent
QQmlEngine engine;
QQmlComponent component(&engine,
        QUrl::fromLocalFile("MyItem.qml"));
QObject *object = component.create();
//![QQmlComponent-a]
}

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

//![QQuickView]
// Using QQuickView
QQuickView view;
view.setSource(QUrl::fromLocalFile("MyItem.qml"));
view.show();
QObject *object = view.rootObject();
//![QQuickView]

//![properties]
object->setProperty("width", 500);
QQmlProperty(object, "width").write(500);
//![properties]

//![cast]
QQuickItem *item = qobject_cast<QQuickItem*>(object);
item->setWidth(500);
//![cast]

//![findChild]
QObject *rect = object->findChild<QObject*>("rect");
if (rect)
    rect->setProperty("color", "red");
//![findChild]

//![QQmlComponent-b]
delete object;
//![QQmlComponent-b]

withComponent();

    return app.exec();
}