updating "text object" setPropery...
-
원본 텍스트
I am trying to change a parameter in my qml file from c++ using the following code:QQmlApplicationEngine engine;
engine.load(QUrl(QLatin1String("qrc:/Window.qml")));QObject text;
text = engine.rootObjects().first()->findChild<QQuickItem>("objectText");qDebug()<<"Before : "<<text->property("text");
QQmlProperty::write(text, "text", QVariant("Change"));
qDebug()<<"After : "<<text->property("text");The debugger gives the output I want.. but the gui isn't updating and stays at the last value. Is there a way to update the gui?
-
원본 텍스트
I am trying to change a parameter in my qml file from c++ using the following code:QQmlApplicationEngine engine;
engine.load(QUrl(QLatin1String("qrc:/Window.qml")));QObject text;
text = engine.rootObjects().first()->findChild<QQuickItem>("objectText");qDebug()<<"Before : "<<text->property("text");
QQmlProperty::write(text, "text", QVariant("Change"));
qDebug()<<"After : "<<text->property("text");The debugger gives the output I want.. but the gui isn't updating and stays at the last value. Is there a way to update the gui?
@LISP said in updating "text object" setPropery...:
QQmlProperty::write(text, "text", QVariant("Change"));
Use QObject instead:
text.setProperty("text", "Change"); -
@LISP said in updating "text object" setPropery...:
QQmlProperty::write(text, "text", QVariant("Change"));
Use QObject instead:
text.setProperty("text", "Change"); -
Are you sure your
textemits a signal when"text"property is changed? -
Did you create 2 QQmlApplicationEngine and load your qml file twice?
Also you should not modify your QML object from c++.
https://doc.qt.io/qt-5/qtquick-bestpractices.html#interacting-with-qml-from-c
http://doc.qt.io/qt-5/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c
https://youtu.be/vzs5VPTf4QQ?t=23m20s@sierdzio as knowledgeable users, I believe we shouldn't foster beginners' bad practices but bring them back in the right track.
@LISP expose a QObject subclass with a QString Q_PROPERTY and bind to that in your objectText object from QML.
When you want to change the text, change the property and emit its notify signal and the QML side will get automatically updated. -
Did you create 2 QQmlApplicationEngine and load your qml file twice?
Also you should not modify your QML object from c++.
https://doc.qt.io/qt-5/qtquick-bestpractices.html#interacting-with-qml-from-c
http://doc.qt.io/qt-5/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c
https://youtu.be/vzs5VPTf4QQ?t=23m20s@sierdzio as knowledgeable users, I believe we shouldn't foster beginners' bad practices but bring them back in the right track.
@LISP expose a QObject subclass with a QString Q_PROPERTY and bind to that in your objectText object from QML.
When you want to change the text, change the property and emit its notify signal and the QML side will get automatically updated.@GrecKo said in updating "text object" setPropery...:
@sierdzio as knowledgeable users, I believe we shouldn't foster beginners' bad practices but bring them back in the right track.
How should I know if it is a beginner? I've seen something like this used to do real magic in a project, and my tough experts.
In general, yes you're right, it is not "the way" to do things in QML projects. But it's not forbidden, and maybe could be useful in some certain edge cases.