How to run SequentialAnimation in the background?
-
I'm designing Qt5.7/QML application. The main idea is that pressing a button will trigger shell command which takes long time. The results of this command should be reflected in widgets.
In the meantime, I need SequentialAnimation to run, to show the user that something is happening in the background. So I start the animation and then call the process which sends the shell command.
But the animation seems to be freezed as the entire GUI until the shell command returns.
I'm a new comer to Qt/QML/C++ (hardware engineer...).
Any suggestions? Can you please post the code for the QML/C++/new header?
QML Code:CircularGauge { id: speedometer_wr x: 199 y: 158 value: valueSource.bps maximumValue: 400 width: height height: container.height * 0.5 stepSize: 0 anchors { verticalCenter: parent.verticalCenter verticalCenterOffset: 46 } style: DashboardGaugeStyle {} NumberAnimation { id: animation_wr running: false target: speedometer_wr property: "value" easing.type: Easing.InOutSine from: 0 to: 300 duration: 15000 } } Button { id: button_benchmark x: 168 y: 26 width: 114 height: 47 text: qsTr("Benchmark") tooltip: "Execute the performance test" checkable: true function handle_becnhmark() { speedometer_wr.value = 0; // Uncheck the button button_benchmark.checked = false; // Start the animation animation_wr.start(); // Run the benchmark var res = backend.run_benchmark(); // Stop the animation animation_wr.stop(); speedometer_wr.value = parseFloat(res.split(",")[0]); } onClicked: {handle_becnhmark()} }
Cpp Code:
#include <QProcess> #include <QtDebug> #include "backend.h" #include <QRegExp> BackEnd::BackEnd(QObject *parent) : QObject(parent) { } QString BackEnd::run_benchmark() { qDebug() << "in run_benchmark"; QProcess proc; // Execute shell command proc.start(<LONG_SHELL_COMMAND>); if (!proc.waitForStarted()) return ""; if (!proc.waitForFinished()) return ""; // Get the results, parse and return values QString result(proc.readAll()); return result; }
backend.h:
#ifndef BACKEND_H #define BACKEND_H #include <QObject> class BackEnd : public QObject { Q_OBJECT public: explicit BackEnd(QObject *parent = nullptr); signals: public slots: QString run_benchmark(); }; #endif // BACKEND_H
-
@Tirupathi-Korla . How to do it? Can you please post example code how to wrap the process as a thread?
-
@bintuch
When you want to run a process, use some thing like
QtConcurrent::run(this, & BackEnd::startaProcess);
and in BackEnd, write
void startaProcess(){ // start a process here.. }
Go through http://doc.qt.io/qt-5/qtconcurrentrun.html#using-member-functions for Qtconcurrent docs.