Passing a QProcess to a function
-
Hi,
How do I pass a new QProcess to a function? I can't get the pointers right... what */& goes where?
Thank you so much!
file.cpp (has qthreadpool to run ProcessThread, need to return value so using connect
QProcess *proc = new QProcess(); ProcessThread *pt = new ProcessThread(node->nodeName(), proc); connect(proc, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(showAnswer()),Qt::QueuedConnection); pool->start(pt); //showAnswer() function in file.cpp
ProcessThread.cpp
ProcessThread::ProcessThread(QString name, QProcess *proc) : x(name), _proc(0) { _proc = proc; } void ProcessThread::run() { }
ProcessThread.h class ProcessThread : public QRunnable { Q_OBJECT public: ProcessThread(QString, QProcess *); void run();
-
@taraj said in Passing a QProcess to a function:
I can't get the pointers right... what */& goes where?
I suggest you read a book about C++ as this is really very basic knowledge.
The code looks correct so far except I do not know how _proc is defined. Do you get any errors?There is another big problem with your code: if you want to use QProcess instance in another thread then either create it in that thread (inside run()) or move it to that thread using https://doc.qt.io/qt-5/qobject.html#moveToThread
-
And the next problem - you don't need a thread when you just want to run a QProcess.
-
@Christian-Ehrlicher yes, currently I have just qprocess but to run the script on a remote machine for at least 50 nodes takes too long and the rest of the GUI application 'sort of' freezes/blocks and can miss the monitoring messages it also receives separately so using threads was a way to try to speed it up
I believe can use signals and slots to communicate back with the main thread—(using QueuedConnection) but can't quite get it going.. didn't seem to like SIGNAL(finished) -
@taraj said in Passing a QProcess to a function:
so using threads was a way to try to speed it up
No! QProcess is async.
and can miss the monitoring messages
No, this can not happen, all stdout/stderr is buffered.
-
I ended up doing it like this:
https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/QProcess was created in the thread and used signal/slot to get the return value from the QThread back to the main GUI thread using emit()
Thanks
-
Still no thread needed for such a task...