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. Get live output (and state) out of QProcess
Forum Updated to NodeBB v4.3 + New Features

Get live output (and state) out of QProcess

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 609 Views 1 Watching
  • 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.
  • S Offline
    S Offline
    Sucharek
    wrote on last edited by
    #1

    Hi, I'm trying to get a live output out of a QProcess.
    I decided to make 2 classes in one header file (thread and download). Both QThreads. One of them I put QProcess in and the other one used for reading/processing the output. But I've got a problem with that. I always get this error:

    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QProcess(0x60fd60), parent's thread is QThread(0x6a7f90), current thread is download(0x60fd50)
    
    this error prints out 3 times
    

    The only way that I found to not get this error is declaring QProcess in in void run();. I currently have it declared as a private member.

    thread.h:

    #ifndef THREAD_H
    #define THREAD_H
    
    #include <QThread>
    #include <QDebug>
    #include <QProcess>
    
    class download : public QThread {
        Q_OBJECT
    
    public:
        bool getState() {return process.state();}
        QString getOut() {return process.readAll();}
    
    private:
        QProcess process;
    
        void run() override {
            process.start("curl", QStringList() << "url");
            process.waitForFinished(-1);
        }
    
    signals:
        void update(QString);
    };
    
    
    class Thread : public QThread
    {
       ...
    private:
        download dl;
    };
    
    #endif // THREAD_H
    

    thread.cpp:

    ...
    qDebug() << "process start";
    dl.start();
    while (dl.getState() != false) {
        qDebug() << dl.getOut();
        msleep(100);
    }
    qDebug() << "finished";
    ...
    
    Christian EhrlicherC JonBJ 2 Replies Last reply
    0
    • S Offline
      S Offline
      Sucharek
      wrote on last edited by Sucharek
      #10

      I managed to get it working.
      This is how I did it:

      download.h

      #ifndef DOWNLOAD_H
      #define DOWNLOAD_H
      
      #include <QObject>
      #include <QProcess>
      #include <QDebug>
      
      class download : public QObject
      {
          Q_OBJECT
      public:
          download();
      
          void Main();
      
      public slots:
          void start();
      
          void stop(int, QProcess::ExitStatus);
      
          void getOutput();
      
      private:
          QProcess process;
      };
      
      #endif // DOWNLOAD_H
      

      download.cpp

      #include "download.h"
      
      download::download()
      {
          QObject::connect(&process, SIGNAL(started()), this, SLOT(start()));
          QObject::connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(stop(int, QProcess::ExitStatus)));
          QObject::connect(&process, SIGNAL(readyReadStandardOutput()), this, SLOT(getOutput()));
      }
      
      void download::start()
      {
          qDebug() << "started";
      }
      
      void download::stop(int, QProcess::ExitStatus)
      {
          qDebug() << "stopped";
      }
      
      void download::getOutput()
      {
          qDebug() << process.readAll();
      }
      
      void download::Main()
      {
          process.start("curl", QStringList() << "url");
          process.waitForStarted();
      }
      
      1 Reply Last reply
      2
      • S Sucharek

        Hi, I'm trying to get a live output out of a QProcess.
        I decided to make 2 classes in one header file (thread and download). Both QThreads. One of them I put QProcess in and the other one used for reading/processing the output. But I've got a problem with that. I always get this error:

        QObject: Cannot create children for a parent that is in a different thread.
        (Parent is QProcess(0x60fd60), parent's thread is QThread(0x6a7f90), current thread is download(0x60fd50)
        
        this error prints out 3 times
        

        The only way that I found to not get this error is declaring QProcess in in void run();. I currently have it declared as a private member.

        thread.h:

        #ifndef THREAD_H
        #define THREAD_H
        
        #include <QThread>
        #include <QDebug>
        #include <QProcess>
        
        class download : public QThread {
            Q_OBJECT
        
        public:
            bool getState() {return process.state();}
            QString getOut() {return process.readAll();}
        
        private:
            QProcess process;
        
            void run() override {
                process.start("curl", QStringList() << "url");
                process.waitForFinished(-1);
            }
        
        signals:
            void update(QString);
        };
        
        
        class Thread : public QThread
        {
           ...
        private:
            download dl;
        };
        
        #endif // THREAD_H
        

        thread.cpp:

        ...
        qDebug() << "process start";
        dl.start();
        while (dl.getState() != false) {
            qDebug() << dl.getOut();
            msleep(100);
        }
        qDebug() << "finished";
        ...
        
        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2
        1. You don't need a thread for this task
        2. You create the QProcess instance in the main thread but access it in the thread created by QThread and therefore the warning - so create it in the thread you're accessing it (= inside run()) - but as already written it's not needed at all - see 1.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • S Sucharek

          Hi, I'm trying to get a live output out of a QProcess.
          I decided to make 2 classes in one header file (thread and download). Both QThreads. One of them I put QProcess in and the other one used for reading/processing the output. But I've got a problem with that. I always get this error:

          QObject: Cannot create children for a parent that is in a different thread.
          (Parent is QProcess(0x60fd60), parent's thread is QThread(0x6a7f90), current thread is download(0x60fd50)
          
          this error prints out 3 times
          

          The only way that I found to not get this error is declaring QProcess in in void run();. I currently have it declared as a private member.

          thread.h:

          #ifndef THREAD_H
          #define THREAD_H
          
          #include <QThread>
          #include <QDebug>
          #include <QProcess>
          
          class download : public QThread {
              Q_OBJECT
          
          public:
              bool getState() {return process.state();}
              QString getOut() {return process.readAll();}
          
          private:
              QProcess process;
          
              void run() override {
                  process.start("curl", QStringList() << "url");
                  process.waitForFinished(-1);
              }
          
          signals:
              void update(QString);
          };
          
          
          class Thread : public QThread
          {
             ...
          private:
              download dl;
          };
          
          #endif // THREAD_H
          

          thread.cpp:

          ...
          qDebug() << "process start";
          dl.start();
          while (dl.getState() != false) {
              qDebug() << dl.getOut();
              msleep(100);
          }
          qDebug() << "finished";
          ...
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #3

          @Sucharek
          Yes, with QProcess as with other QObjects you cannot have it live in one thread and access it from a different thread.

          Why do you need threads for any of this, given that QProcess offers fully asynchronous operation for e.g. reading? Get rid of waitForFinished(), get rid of msleep()s, just use readRead() & finished() signals from QProcess.

          1 Reply Last reply
          1
          • S Offline
            S Offline
            Sucharek
            wrote on last edited by
            #4

            Oh ok.
            Well, I tried doing it like this:

            QProcess process;
                    process.start("curl", QStringList() << "url");
                    while (process.state() == QProcess::Running) {
                        qDebug() << process.readAll(); //prints nothing
                        msleep(100);
                    }
                    qDebug() << process.readAll();
            

            The while loop is looping forever.

            @JonB, you said to use the finished() signal, but I'm not sure how to work with it. Should I use QObject::connect()? If yes, how?

            Christian EhrlicherC 1 Reply Last reply
            0
            • S Sucharek

              Oh ok.
              Well, I tried doing it like this:

              QProcess process;
                      process.start("curl", QStringList() << "url");
                      while (process.state() == QProcess::Running) {
                          qDebug() << process.readAll(); //prints nothing
                          msleep(100);
                      }
                      qDebug() << process.readAll();
              

              The while loop is looping forever.

              @JonB, you said to use the finished() signal, but I'm not sure how to work with it. Should I use QObject::connect()? If yes, how?

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @Sucharek said in Get live output (and state) out of QProcess:

              The while loop is looping forever.

              As already told you - use the proper QProcess signals .

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              S 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @Sucharek said in Get live output (and state) out of QProcess:

                The while loop is looping forever.

                As already told you - use the proper QProcess signals .

                S Offline
                S Offline
                Sucharek
                wrote on last edited by Sucharek
                #6

                @Christian-Ehrlicher alright, I tried using finished(), but it also doesn't work.
                This is what I have:

                QProcess process;
                process.start("curl", QStringList() << "url");
                
                QObject::connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(stop(int, QProcess::ExitStatus)));
                
                while (processRunning == 1) {
                    qDebug() << "running";
                    msleep(100);
                }
                

                I get this error:

                QObject::connect: Cannot queue arguments of type 'QProcess::ExitStatus'
                (Make sure 'QProcess::ExitStatus' is registered using qRegisterMetaType().)
                
                Christian EhrlicherC 1 Reply Last reply
                0
                • S Sucharek

                  @Christian-Ehrlicher alright, I tried using finished(), but it also doesn't work.
                  This is what I have:

                  QProcess process;
                  process.start("curl", QStringList() << "url");
                  
                  QObject::connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(stop(int, QProcess::ExitStatus)));
                  
                  while (processRunning == 1) {
                      qDebug() << "running";
                      msleep(100);
                  }
                  

                  I get this error:

                  QObject::connect: Cannot queue arguments of type 'QProcess::ExitStatus'
                  (Make sure 'QProcess::ExitStatus' is registered using qRegisterMetaType().)
                  
                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  Again: you don't need threads so don't use them. Create a QProcess instance, start the application you want and connect the finished() signal to your slot to read out the data you want.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  S 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    Again: you don't need threads so don't use them. Create a QProcess instance, start the application you want and connect the finished() signal to your slot to read out the data you want.

                    S Offline
                    S Offline
                    Sucharek
                    wrote on last edited by
                    #8

                    @Christian-Ehrlicher I'm gonna be using that thread for something else. Should I create a new class just for QProcess?

                    Christian EhrlicherC 1 Reply Last reply
                    0
                    • S Sucharek

                      @Christian-Ehrlicher I'm gonna be using that thread for something else. Should I create a new class just for QProcess?

                      Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #9

                      @Sucharek said in Get live output (and state) out of QProcess:

                      I'm gonna be using that thread for something else

                      But the way you showed in your first post you're doing it completely wrong and we already told you so. Please read the documentation.

                      Should I create a new class just for QProcess?

                      No - why should you create a class for QProcess? Don't understand this question.

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        Sucharek
                        wrote on last edited by Sucharek
                        #10

                        I managed to get it working.
                        This is how I did it:

                        download.h

                        #ifndef DOWNLOAD_H
                        #define DOWNLOAD_H
                        
                        #include <QObject>
                        #include <QProcess>
                        #include <QDebug>
                        
                        class download : public QObject
                        {
                            Q_OBJECT
                        public:
                            download();
                        
                            void Main();
                        
                        public slots:
                            void start();
                        
                            void stop(int, QProcess::ExitStatus);
                        
                            void getOutput();
                        
                        private:
                            QProcess process;
                        };
                        
                        #endif // DOWNLOAD_H
                        

                        download.cpp

                        #include "download.h"
                        
                        download::download()
                        {
                            QObject::connect(&process, SIGNAL(started()), this, SLOT(start()));
                            QObject::connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(stop(int, QProcess::ExitStatus)));
                            QObject::connect(&process, SIGNAL(readyReadStandardOutput()), this, SLOT(getOutput()));
                        }
                        
                        void download::start()
                        {
                            qDebug() << "started";
                        }
                        
                        void download::stop(int, QProcess::ExitStatus)
                        {
                            qDebug() << "stopped";
                        }
                        
                        void download::getOutput()
                        {
                            qDebug() << process.readAll();
                        }
                        
                        void download::Main()
                        {
                            process.start("curl", QStringList() << "url");
                            process.waitForStarted();
                        }
                        
                        1 Reply Last reply
                        2
                        • S Sucharek has marked this topic as solved on

                        • Login

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