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. QProcess process finish call back function
Forum Updated to NodeBB v4.3 + New Features

QProcess process finish call back function

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt qprocess
12 Posts 5 Posters 15.3k Views 3 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.
  • RiteshPanchalR Offline
    RiteshPanchalR Offline
    RiteshPanchal
    wrote on last edited by
    #1

    I am using Qt 5.2.1 on ubuntu 14.04.
    I am using Qprocess to run Linux Commands.
    I want to call a function when process completed or any error occurred instead of waiting for process to be completed.
    I found that can be done using following signal

    connect(process, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
        [=](int exitCode, QProcess::ExitStatus exitStatus){ /* ... */ });
    

    But i unable to understand the syntax. How can i modify to call a function when process is finished?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Shahina
      wrote on last edited by
      #2

      Try like this :

      QProcess *process = new QProcess(this);
      connect(process , SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(processFinished(int, QProcess::ExitStatus)));

      // ur own slot
      void ClassName :: processFinished(int code , QProcess::ExitStatus status)
      {
      // call ur function;
      }

      RiteshPanchalR 1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Hi
        Just as note.
        That syntax is a c++ lambda. A in place function.
        So it does call a function but unlike @Shahina sample where the slot is a stand alone
        function, the lambda is in same place as the connect.

        connect(process, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
        
        [=]  (int exitCode, QProcess::ExitStatus exitStatus) 
        {
           Here is function body
        }
        
        );
        

        http://www.drdobbs.com/cpp/lambdas-in-c11/240168241

        Also note that this syntax require a c++11 enabled compiler.

        1 Reply Last reply
        1
        • BjornWB Offline
          BjornWB Offline
          BjornW
          wrote on last edited by BjornW
          #4

          There are multiple ways to connect signals to different receivers. You are using the functor version (http://doc.qt.io/qt-5/qobject.html#connect-4):

          QMetaObject::Connection QObject::connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
          

          So the last argument of the call is the method to be called when the signal fires. This is where you need to specify what to call. The last argument you provided looks like this:

           [=](int exitCode, QProcess::ExitStatus exitStatus){ /* ... */ }
          

          Right now it is an empty function. You simply need to add a method call in here.

           [=](int exitCode, QProcess::ExitStatus exitStatus){ onProcessFinished(); }
          

          The object you are are connecting to seems to be a QObject so you can use the following syntax instead (which i prefer)

           connect(process, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), this, &MyClassName::onProcessFinished);
          

          in either case you need to add the onProcessFinished method to your class.

          Edit: mrjj got here first :-)

          1 Reply Last reply
          2
          • S Shahina

            Try like this :

            QProcess *process = new QProcess(this);
            connect(process , SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(processFinished(int, QProcess::ExitStatus)));

            // ur own slot
            void ClassName :: processFinished(int code , QProcess::ExitStatus status)
            {
            // call ur function;
            }

            RiteshPanchalR Offline
            RiteshPanchalR Offline
            RiteshPanchal
            wrote on last edited by
            #5

            @Shahina
            gstpipe.cpp

            #include "gstpipe.h"
            
            gstPipe::gstPipe(QObject *parent) :
                QObject(parent)
            {
            }
            
            void gstPipe::playVideo(QString location)
            {
                connect(process , SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(processFinished(int, QProcess::ExitStatus)));
            }
            
            void gstPipe::processFinished(int code , QProcess::ExitStatus status)
            {
                process.kill();
            }
            

            gstpipe.h

            #ifndef GSTPIPE_H
            #define GSTPIPE_H
            
            #include <QObject>
            #include <QProcess>
            
            
            class gstPipe : public QObject
            {
                Q_OBJECT
            public:
                explicit gstPipe(QObject *parent = 0);
            
            public Q_SLOTS:
            
            private:
                QProcess process;
            
            signals:
                void processFinished(int code , QProcess::ExitStatus status);
            
            public slots:
            
            };
            
            #endif // GSTPIPE_H
            

            I am getting following error. Whats missing?

            error: no matching function for call to 'gstPipe::connect(QProcess&, const char*, gstPipe* const, const char*)'
                connect(process , SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(processFinished(int, QProcess::ExitStatus)));
            

            Actually i started learning directly QML so i am new to Signal and Slots in QT C++.
            ^

            1 Reply Last reply
            0
            • RiteshPanchalR Offline
              RiteshPanchalR Offline
              RiteshPanchal
              wrote on last edited by
              #6

              if i use following command

              connect(process, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
                     [=](int exitCode, QProcess::ExitStatus exitStatus){ /* ... */ });
              

              I am getting below error

              warning: lambda expressions only available with -std=c++11 or -std=gnu++11 [enabled by default]
                       [=](int exitCode, QProcess::ExitStatus exitStatus){ /* ... */ });
                                                                                     ^
              error: no matching function for call to 'gstPipe::connect(QProcess&, void (QProcess::*)(int, QProcess::ExitStatus), gstPipe::playVideo(QString)::__lambda0)'
                       [=](int exitCode, QProcess::ExitStatus exitStatus){ /* ... */ });
                                                                                      ^
              
              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                You need to add CONFIG += c++11 to your .pro file.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                RiteshPanchalR 1 Reply Last reply
                0
                • SGaistS SGaist

                  Hi,

                  You need to add CONFIG += c++11 to your .pro file.

                  RiteshPanchalR Offline
                  RiteshPanchalR Offline
                  RiteshPanchal
                  wrote on last edited by
                  #8

                  @SGaist
                  After Adding CONFIG += c++11 solved warning: lambda expressions only available with -std=c++11 or -std=gnu++11 [enabled by default] warning. But still getting no matching function error.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Shahina
                    wrote on last edited by
                    #9

                    U can declare the processFinished(int, QProcess::ExitStatus) function under private slots in header file like :

                    gstpipe.h :

                    private slots:
                    void processFinished(int, QProcess:ExitStatus);

                    RiteshPanchalR 1 Reply Last reply
                    0
                    • S Shahina

                      U can declare the processFinished(int, QProcess::ExitStatus) function under private slots in header file like :

                      gstpipe.h :

                      private slots:
                      void processFinished(int, QProcess:ExitStatus);

                      RiteshPanchalR Offline
                      RiteshPanchalR Offline
                      RiteshPanchal
                      wrote on last edited by
                      #10

                      @Shahina
                      I tried declaring under private slots: in gstpipe.h but still getting the same error.

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        Shahina
                        wrote on last edited by
                        #11

                        If QProcess is not a pointer object means, u will need to pass reference of QProcess
                        Eg : connect(&process, SIGNAL(), this, SLOT());

                        RiteshPanchalR 1 Reply Last reply
                        1
                        • S Shahina

                          If QProcess is not a pointer object means, u will need to pass reference of QProcess
                          Eg : connect(&process, SIGNAL(), this, SLOT());

                          RiteshPanchalR Offline
                          RiteshPanchalR Offline
                          RiteshPanchal
                          wrote on last edited by
                          #12

                          @Shahina
                          yeah that's my mistake.

                          QObject::connect(&process , SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(processFinished(int, QProcess::ExitStatus)));
                          

                          This will compiles without any error.
                          Thanks for your great help.

                          1 Reply Last reply
                          0

                          • Login

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