(Editted to add more context)
I've started using QML and I'd like to set some sort of reference property on a QML type, linking two QML objects (ideally, without a parent/child relationship as I'd like to connect multiple QML objects).
For example, I have the following files.
qmldir:
A 1.0 A.qml
A.qml
import QtQuick 2.2
Rectangle {
width: 100
height: 100
color: 'red'
// Other stuff later
}
main.qml:
import QtQuick 2.2
import QtQuick.Window 2.1
import "qrc:/"
Rectangle {
objectName: "Main window"
visible: true
width: 360
height: 360
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
}
}
Text {
text: qsTr("Hello World")
anchors.centerIn: parent
}
property A referencedObject;
Rectangle {
id: subView
objectName: "subView"
}
}
What I'd like to do with this is set the value of 'referencedObject' to an object created by C++ at runtime. However, the function for setting properties doesn't allow for QObject pointers or QQuickItem pointers, as QVariant objects can't be constructed that way.
Something like:
QQmlComponent aTemplate(&engine, QString("qrc:/A.qml"), &parentObject);
QQuickItem* aInstance = aTemplate.create();
aInstance->setParent(&parentObject);
mainView.setProperty("referencedObject",aInstance); // Won't work.
I'd like to keep the 'A' type object in QML because a) less boilerplate than C++ for this, and b) its meant to be a separate graphical object with life of its own, and QML works better for that use-case.
Thanks for any help posted.