PDA

View Full Version : QProcess Signal Connection and Type



rhb327
5th February 2020, 21:58
Suppose a QProcess is started() from the main/GUI thread. And suppose the new processes finished() signal is connected to a SLOT in the main/GUI thread. Please confirm this will use a Qt::queuedConnection type.

I'm almost certain it does as QProcess should be in another thread by definition.

Thanks,
-Rich

ChristianEhrlicher
6th February 2020, 19:57
Please confirm this will use a Qt::queuedConnection type.

No, there is no need for it


I'm almost certain it does as QProcess should be in another thread by definition.

Why?

rhb327
6th February 2020, 20:47
By definition a spawned process would run as a different process (and therefore a different thread). In my case the main/GUI thread spawns the external process.

Within a Qt application / process there can be multiple threads and it's typical that these intra-process threads would communicate with a QueuedConnection signal / slot. Thus, the thinking was surely a separate process would signal another Qt process using a queued connection but that does not appear to be the case.

In my sample below the queuedconnection throws:

QObject::connect: Cannot queue arguments of type 'QProcess::ExitStatus'
(Make sure 'QProcess::ExitStatus' is registered using qRegisterMetaType().)

...but the DirectConnection or AutoConnection does not. So my thought now is that QProcess is owned by the main/GUI thread and signaling from within the main/GUI thread would be direct. So finished() is truly emitted by the main/GUI thread in this case. However, I'm now curious how Qt was made aware that the external process was indeed finished. I think there must be an inter process communication that occurs prior and then the main/GUI thread issues finished() as a direct connection within the main/GUI thread.

Thanks,
-Rich

Sample...



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

//connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(pFinished(int, QProcess::ExitStatus)));
connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(pFinished(int, QProcess::ExitStatus)), Qt::QueuedConnection);

process.setWorkingDirectory("C:/Users/richard.bair/Downloads/");
QStringList arguments = QStringList() << "/K" << "dir";
process.setCreateProcessArgumentsModifier([] (QProcess::CreateProcessArguments *args)
{
args->flags |= CREATE_NEW_CONSOLE;
args->startupInfo->dwFlags &= ~STARTF_USESTDHANDLES;
});
process.start("cmd.exe", arguments);


Added after 31 minutes:

Yeah, reading here: https://doc.qt.io/qt-5/ipc.html strongly suggests the "API" for QProcess is where the comms occur form the external process back to my Qt process and then once that comms has occurred then QProcess (owned by main/GUI thread) issues a finished() signal in the same thread so a direct connection is used.

ChristianEhrlicher
7th February 2020, 05:24
QProcess just starts another process and handles it and is running in your process. There is no need that this handling is done in a separate thread.