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 "text object" setPropery...
Qt 6.11 is out! See what's new in the release blog

updating "text object" setPropery...

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 3 Posters 896 Views 1 Watching
  • 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.
  • L Offline
    L Offline
    LISP
    wrote on last edited by
    #1

    원본 텍스트
    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?

    sierdzioS 1 Reply Last reply
    0
    • L LISP

      원본 텍스트
      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?

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @LISP said in updating "text object" setPropery...:

      QQmlProperty::write(text, "text", QVariant("Change"));

      Use QObject instead:

      text.setProperty("text", "Change");
      

      (Z(:^

      L 1 Reply Last reply
      0
      • sierdzioS sierdzio

        @LISP said in updating "text object" setPropery...:

        QQmlProperty::write(text, "text", QVariant("Change"));

        Use QObject instead:

        text.setProperty("text", "Change");
        
        L Offline
        L Offline
        LISP
        wrote on last edited by
        #3

        @sierdzio

        Thank you sir!
        text.setProperty("text", "Change");
        This is the first method I've used, but still no response.
        Will others be updated right away?

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Are you sure your text emits a signal when "text" property is changed?

          (Z(:^

          1 Reply Last reply
          0
          • GrecKoG Offline
            GrecKoG Offline
            GrecKo
            Qt Champions 2018
            wrote on last edited by
            #5

            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.

            sierdzioS 1 Reply Last reply
            0
            • GrecKoG GrecKo

              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.

              sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

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

              (Z(:^

              1 Reply Last reply
              0

              • Login

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