Update qml component dynamically using C++
-
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