Unsolved QProcess process finish call back function
-
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 signalconnect(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?
-
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;
} -
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.
-
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 :-)
-
@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++.
^ -
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){ /* ... */ }); ^
-
Hi,
You need to add
CONFIG += c++11
to your .pro file. -
@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. -
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); -
@Shahina
I tried declaring under private slots: in gstpipe.h but still getting the same error. -
If QProcess is not a pointer object means, u will need to pass reference of QProcess
Eg : connect(&process, SIGNAL(), this, SLOT()); -
@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.