Quantcast
Channel: Qt DevNet forums: Qt Quick 1283365070**
Viewing all articles
Browse latest Browse all 4972

[Solved] Can't dynamically add and display objects to QML from C++ (Qt 5.02)

$
0
0
Hi all, I’ve been trying to learn how to dynamically add objects to QML from C++ code, but all the code I write doesn’t work. I want to add an object defined in Unit.qml to the application’s window (defined in generated file main.qml). I created a new Qt Quick 2 application (by Create Project —> Applications —> Qt Quick 2 Application (Built-in Elements)), added a method for creating QML objects in class QtQuick2ApplicationViewer (the class was generated by Qt Creator), and called this method from main(). Then tried to follow this [qt-project.org] article, so the method I added to QtQuick2ApplicationViewer looked like this: void QtQuick2ApplicationViewer::addQmlObject() {     QQmlComponent * component = new QQmlComponent(this->engine(), QUrl::fromLocalFile("Unit.qml"), this);       QObject * object = component->create(); } Here’s Unit.qml code: import QtQuick 2.0   Item {     id: unit     width: 40     height: 40       Image {         id: image         width: parent.width         height: parent.height         anchors.fill: parent         source: "content/unit.png"     } } And it doesn’t work. Probably the problem is in my misunderstanding of QML-C++ interacting, but I can’t figure out what’s wrong. Could anyone help me, please?

Viewing all articles
Browse latest Browse all 4972

Trending Articles