Updating qml child label element text after completing a function in C++
-
wrote on 27 May 2014, 09:26 last edited by
I have an application where when after finish executing a function in C++, i need to update the a label in QML with C++ function status.
Below is the C++ code where it calls qml function to let know about it's status.
@ void UiHandler::updateStatus(QVariant msg)
{
QQmlEngine engine;
QQmlComponent component(&engine, "qml/Window.qml");
QObject *object = component.create();QMetaObject::invokeMethod(object, "**StatusUpdate**", Q_ARG(QVariant, msg)); delete object; }@
And my QML looks like below
@ import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Layouts 1.1Rectangle { id: container property alias StatusLabel: ui_label_status Component.onCompleted: { console.debug("Window>> onCompleted"); } onVisibleChanged: { console.debug("Window>> onVisibleChanged"); } function **StatusUpdate**(msg) { **ui_label_status**.text = "Click the button to start" ui_label_status.color = "blue" console.log("Got message:", msg) } color: "lightgreen" ColumnLayout { id: settings_layout anchors.fill: parent GroupBox { id: group anchors { top: parent.top topMargin: 25 left: parent.left leftMargin: 10 } height: children.height width: children.width title: "Test" GridLayout { id: layout anchors.fill: parent Label { id: ui_label_status } } } } }@
Now, when i tried to change the text of my label (ui_label_status) inside the function StatusUpdate it has no effect. Could someone point me how I can update the label text for my scenario above ? And also i am on QT 5.2.
-
wrote on 27 May 2014, 16:19 last edited by
I have to ask...
Have you actually tried running this code?
A number of issues here.
Firstly you are creating an instance of "qml/Window.qml" and then deleting it at the end of your method call so any changes you make will obviously be lost.
Secondly you create a QQmlEngine on stack which would be invalid to use to create new components.
Thirdly your Qml does not compile.
-
wrote on 27 May 2014, 16:32 last edited by
The following works for me:
main.qml
@import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Layouts 1.1Rectangle {
id: container
property alias statusLabel: ui_label_statusComponent.onCompleted: {
console.debug("Window>> onCompleted");
}onVisibleChanged: {
console.debug("Window>> onVisibleChanged");
}function statusUpdate(msg) {
ui_label_status.text = "Click the button to start"
ui_label_status.color = "blue"
console.log("Got message:", msg)
}color: "lightgreen"
ColumnLayout {
id: settings_layout
anchors.fill: parentGroupBox { id: group anchors { top: parent.top topMargin: 25 left: parent.left leftMargin: 10 } height: children.height width: children.width title: "Test" GridLayout { id: layout anchors.fill: parent Label { id: ui_label_status } } }
}
}@main.cpp
@#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);QtQuick2ApplicationViewer viewer; viewer.setMainQmlFile(QStringLiteral("qml/Testing/main.qml")); viewer.showExpanded(); QMetaObject::invokeMethod((QObject*)viewer.rootObject(), "statusUpdate", Q_ARG(QVariant, "TESTING")); return app.exec();
}@
-
wrote on 28 May 2014, 00:18 last edited by
Another approach is to declare a property in C++ object, register the class or an instance with the QML engine, and emit the property's update signal when the value should be re-read from QML.
1/4