Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Updating qml child label element text after completing a function in C++
Forum Update on Monday, May 27th 2025

Updating qml child label element text after completing a function in C++

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 3.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    qtdevana
    wrote on 27 May 2014, 09:26 last edited by
    #1

    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.1

    Rectangle {
        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.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      melghawi
      wrote on 27 May 2014, 16:19 last edited by
      #2

      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.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        melghawi
        wrote on 27 May 2014, 16:32 last edited by
        #3

        The following works for me:

        main.qml

        @import QtQuick 2.2
        import QtQuick.Controls 1.1
        import QtQuick.Layouts 1.1

        Rectangle {
        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
                  }
              }
          }
        

        }
        }@

        main.cpp

        @#include <QtGui/QGuiApplication>
        #include "qtquick2applicationviewer.h"

        int main(int argc, char *argv[])
        {
        QGuiApplication app(argc, argv);

        QtQuick2ApplicationViewer viewer;
        viewer.setMainQmlFile&#40;QStringLiteral("qml/Testing/main.qml"&#41;);
        viewer.showExpanded();
        
        QMetaObject::invokeMethod((QObject*)viewer.rootObject(), "statusUpdate", Q_ARG(QVariant, "TESTING"));
        
        return app.exec();
        

        }@

        1 Reply Last reply
        0
        • jeremy_kJ Offline
          jeremy_kJ Offline
          jeremy_k
          wrote on 28 May 2014, 00:18 last edited by
          #4

          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.

          Asking a question about code? http://eel.is/iso-c++/testcase/

          1 Reply Last reply
          0

          1/4

          27 May 2014, 09:26

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved