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. How to run SequentialAnimation in the background?
Qt 6.11 is out! See what's new in the release blog

How to run SequentialAnimation in the background?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 1.6k 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.
  • B Offline
    B Offline
    bintuch
    wrote on last edited by bintuch
    #1

    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
    
    T 1 Reply Last reply
    0
    • B bintuch

      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
      
      T Offline
      T Offline
      Tirupathi Korla
      wrote on last edited by
      #2

      @bintuch
      You may run the process in a thread.

      B 1 Reply Last reply
      0
      • T Tirupathi Korla

        @bintuch
        You may run the process in a thread.

        B Offline
        B Offline
        bintuch
        wrote on last edited by
        #3

        @Tirupathi-Korla . How to do it? Can you please post example code how to wrap the process as a thread?

        T 1 Reply Last reply
        0
        • B bintuch

          @Tirupathi-Korla . How to do it? Can you please post example code how to wrap the process as a thread?

          T Offline
          T Offline
          Tirupathi Korla
          wrote on last edited by
          #4

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

          1 Reply Last reply
          1

          • Login

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