Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Update qml component dynamically using C++

Update qml component dynamically using C++

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 366 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.
  • J Offline
    J Offline
    JJLim
    wrote on last edited by
    #1

    Hi, may I know how can we update the value of a qml component (for example Progress Bar) dynamically using C++? For example, the value of the qml component will be updated for an interval of 1min.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bludger
      wrote on last edited by bludger
      #2

      Well, you have a couple of options here. I'd like to recommend the following way:
      Add a property to a QObject derived class and make sure you can access it from QML by registering it as a context property.

      Example:

      qmlEngine.rootContext()->setContextProperty("ProgressReporter", ptrToProgressReport);
      

      Make sure you add the property to your ProgressReport class and implement

      class ProgressReporter : public QObject {
       Q_OBJECT
      
       Q_PROPERTY(int progress READ progress NOTIFY progressChanged)
      
      public:
        void doStuff() {
          while (true) {
            m_progress += 1;
            emit progressChanged();
            QThread::msleep(1000);
          }
        }
        int progress() { return m_progress; }
      
      signals:
        void progressChanged();
      };
      

      And bind the progress property to the value property of your progress bar:

      ProgressBar {
        from: 0
        to: 100
        value: ProgressReporter.progress
      }
      

      Hope this helps

      J.HilkJ 1 Reply Last reply
      0
      • B bludger

        Well, you have a couple of options here. I'd like to recommend the following way:
        Add a property to a QObject derived class and make sure you can access it from QML by registering it as a context property.

        Example:

        qmlEngine.rootContext()->setContextProperty("ProgressReporter", ptrToProgressReport);
        

        Make sure you add the property to your ProgressReport class and implement

        class ProgressReporter : public QObject {
         Q_OBJECT
        
         Q_PROPERTY(int progress READ progress NOTIFY progressChanged)
        
        public:
          void doStuff() {
            while (true) {
              m_progress += 1;
              emit progressChanged();
              QThread::msleep(1000);
            }
          }
          int progress() { return m_progress; }
        
        signals:
          void progressChanged();
        };
        

        And bind the progress property to the value property of your progress bar:

        ProgressBar {
          from: 0
          to: 100
          value: ProgressReporter.progress
        }
        

        Hope this helps

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        thank you @bludger for taking your time and providing an example for the OP.

        But please for all our futures sakes , change it to a QTimer based example, no need to seed bad coding style for an event driven framework like Qt


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        B 1 Reply Last reply
        1
        • J Offline
          J Offline
          JJLim
          wrote on last edited by
          #4

          @bludger Wow cool, thks for your explanation

          @J-Hilk And also thks for your advice

          ^.^

          1 Reply Last reply
          0
          • J.HilkJ J.Hilk

            thank you @bludger for taking your time and providing an example for the OP.

            But please for all our futures sakes , change it to a QTimer based example, no need to seed bad coding style for an event driven framework like Qt

            B Offline
            B Offline
            bludger
            wrote on last edited by
            #5

            @J-Hilk Thanks for judging my skills based on one example, really appreciated.

            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