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. Update Label Text with QML method called from C++
QtWS25 Last Chance

Update Label Text with QML method called from C++

Scheduled Pinned Locked Moved QML and Qt Quick
property alias
4 Posts 2 Posters 3.6k 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.
  • M Offline
    M Offline
    marcosbontempo
    wrote on 1 Jun 2015, 13:57 last edited by
    #1

    Hello,

    I wrote a QML function which only updates a label text:

    property alias textchange: label1
    
    function myQmlFunction(msg) {
        item.textchange.text = msg
        console.log(item.textchange.text)
        return
    }
    Label {
            id: label1
            objectName: "labelreceived"
            x: 23
            y: 71
            text: "received"
    }   
    

    When the myQmlFunction is called from an onClicked button event it works. But it doesn't when invoked from C++ method. It reads correctly the text object but can't write. It looks like the function only works when called from a QML code.

    I can assure that there is no problem to invoke the function from C++, it runs and logs at console. The problem is that the text label isn't updated in the screen.

    Is there a issue about updating a QML component property from C++?
    Here is the C++ code:

    QQuickView view;
    view.setSource(QUrl::fromLocalFile("qml/sisc2/main.qml"));
    QObject *object = view.rootObject();
    QMetaObject::invokeMethod(object, "myQmlFunction", Q_ARG(QVariant, "hello"));
    

    Any tip will be very helpful.
    Thanks.

    P 1 Reply Last reply 1 Jun 2015, 16:25
    0
    • M marcosbontempo
      1 Jun 2015, 13:57

      Hello,

      I wrote a QML function which only updates a label text:

      property alias textchange: label1
      
      function myQmlFunction(msg) {
          item.textchange.text = msg
          console.log(item.textchange.text)
          return
      }
      Label {
              id: label1
              objectName: "labelreceived"
              x: 23
              y: 71
              text: "received"
      }   
      

      When the myQmlFunction is called from an onClicked button event it works. But it doesn't when invoked from C++ method. It reads correctly the text object but can't write. It looks like the function only works when called from a QML code.

      I can assure that there is no problem to invoke the function from C++, it runs and logs at console. The problem is that the text label isn't updated in the screen.

      Is there a issue about updating a QML component property from C++?
      Here is the C++ code:

      QQuickView view;
      view.setSource(QUrl::fromLocalFile("qml/sisc2/main.qml"));
      QObject *object = view.rootObject();
      QMetaObject::invokeMethod(object, "myQmlFunction", Q_ARG(QVariant, "hello"));
      

      Any tip will be very helpful.
      Thanks.

      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 1 Jun 2015, 16:25 last edited by
      #2

      Hi @marcosbontempo
      Is this the only code ? I see no problem in above code.

      157

      1 Reply Last reply
      0
      • M Offline
        M Offline
        marcosbontempo
        wrote on 2 Jun 2015, 11:39 last edited by
        #3

        Hi @p3c0,
        Thanks for answering. I found out my mistake. I created a new QQuickView component in a class, different from the main viewer. So, when I changed a component property, I was changing a the property of a component from another viewer I've created.

        Sorry for the the dummie issue!

        P 1 Reply Last reply 3 Jun 2015, 05:01
        0
        • M marcosbontempo
          2 Jun 2015, 11:39

          Hi @p3c0,
          Thanks for answering. I found out my mistake. I created a new QQuickView component in a class, different from the main viewer. So, when I changed a component property, I was changing a the property of a component from another viewer I've created.

          Sorry for the the dummie issue!

          P Offline
          P Offline
          p3c0
          Moderators
          wrote on 3 Jun 2015, 05:01 last edited by
          #4

          @marcosbontempo That's fine.. It happens all the time :)

          157

          1 Reply Last reply
          0

          1/4

          1 Jun 2015, 13:57

          • 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