How to properly set a property of a Qml item via a QQuickView instance ?
-
Hi, Qt'ers !
I have three files:
-
BDialog.qml
- item definition -
BEditorDialog.qml
- aBDialog
with a buttons and checkbox -
main.cpp - C++
The content is the following:
// main.cpp #include <QApplication> #include <QQuickView> #include <QQmlApplicationEngine> #include <QQmlContext> class Controller : public QObject { Q_OBJECT public slots: bool getStatus() { return true; } }; int main( int argc, char **argv ) { QApplication app( argc, argv ); Controller controller; QQuickView view; view.setSource( QUrl::fromLocalFile( "qrc:/BEditorDialog.qml" ) ); view.rootContext()->setContextProperty( "controller", QVariant::fromValue( &controller ) ); view.rootContext()->setContextProperty( "buttonText", "Text" ); view.show(); return app.exec(); } #include "main.moc"
//BDialog.qml import QtQuick 2.6 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.3 Item { property QtObject controller property string buttonText }
//BEditorDialog.qml import QtQuick 2.6 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.3 BDialog { id: root RowLayout { CheckBox { text: "Check status" checked: root.controller.getStatus() } Button { text: root.buttonText } } }
I compile and run this but the combobox isn't checked and there is no text on the button, also I have (obviously) got the error message:
qrc:/BEditorDialog.qml:11: TypeError: Cannot call method 'getStatus' of null
The problem disappears if I remove the
root.
prefix in theBEditorDialog.qml
:root.controller -> controller
together with the removing of property declaration from theBDialog.qml
but I want to set a property of a "base" qml -- and not create a new one.So could you give me a direction, I've read some documentation but haven't found a solution. I think this should be done via the
setInitialProperties
member function but I have only Qt 5.12.Thank you in advance.
Platform: linux
Qt: 5.12 -
-
Hi, Qt'ers !
I have three files:
-
BDialog.qml
- item definition -
BEditorDialog.qml
- aBDialog
with a buttons and checkbox -
main.cpp - C++
The content is the following:
// main.cpp #include <QApplication> #include <QQuickView> #include <QQmlApplicationEngine> #include <QQmlContext> class Controller : public QObject { Q_OBJECT public slots: bool getStatus() { return true; } }; int main( int argc, char **argv ) { QApplication app( argc, argv ); Controller controller; QQuickView view; view.setSource( QUrl::fromLocalFile( "qrc:/BEditorDialog.qml" ) ); view.rootContext()->setContextProperty( "controller", QVariant::fromValue( &controller ) ); view.rootContext()->setContextProperty( "buttonText", "Text" ); view.show(); return app.exec(); } #include "main.moc"
//BDialog.qml import QtQuick 2.6 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.3 Item { property QtObject controller property string buttonText }
//BEditorDialog.qml import QtQuick 2.6 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.3 BDialog { id: root RowLayout { CheckBox { text: "Check status" checked: root.controller.getStatus() } Button { text: root.buttonText } } }
I compile and run this but the combobox isn't checked and there is no text on the button, also I have (obviously) got the error message:
qrc:/BEditorDialog.qml:11: TypeError: Cannot call method 'getStatus' of null
The problem disappears if I remove the
root.
prefix in theBEditorDialog.qml
:root.controller -> controller
together with the removing of property declaration from theBDialog.qml
but I want to set a property of a "base" qml -- and not create a new one.So could you give me a direction, I've read some documentation but haven't found a solution. I think this should be done via the
setInitialProperties
member function but I have only Qt 5.12.Thank you in advance.
Platform: linux
Qt: 5.12You haven't set the property for the loaded qml file.
You may be able to se it like this:
itemLoader.setSource("Test.qml", {"stringa1": "ScrivoStr1", "stringa2": "ScrivoStr2"})
Or in the parent qml set the property to the context property you configured in the backend.
Property QObject myObject: myObject
-