Update qml component dynamically using C++
-
wrote on 3 Sept 2021, 08:44 last edited by
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.
-
wrote on 3 Sept 2021, 09:01 last edited by bludger 9 Mar 2021, 09:11
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
-
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
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
-
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
wrote on 3 Sept 2021, 10:12 last edited by@J-Hilk Thanks for judging my skills based on one example, really appreciated.
3/5