I am experiencing with the new QQuickWidget. How can I interact between the QQuickWidget and C++?
C++
QQuickWidget *view = new QQuickWidget();
view->setSource(QUrl::fromLocalFile("myqml.qml"));
view->setProperty("test", 0);
myLayout->addWidget(view);
QML
import QtQuick 2.1
Rectangle {
id: mainWindow
width: parent.width
height: parent.height
Text {
id: text
width: mainWindow.width
font.pixelSize: 20
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
text: test
}
}
text: test
does not work: ReferenceError: test is not defined
How can I give my QML file some properties via C++?
Is it also possible to get the Text object in C++ and update its text?
Give it a try:
instead of
setProperty(name, val)
works if object has the propertyname
defined asQ_PROPERTY
.It is possible to pass
QObject
-derived object asview
's context property:In general, it doesn't seem to be the best approach --
c++
code shouldn't be aware of presentation if it follows model-view pattern.However it is possible as described here.