I am using QProcess
for the first time, and while experimenting I noticed that readyRead
is signaled before started
.
I tried running /usr/bin/echo foobar
with QProcess
, and attached two functions to readyRead()
and started()
signals, and I get output in this order:
- process start with start() method
- dataReady() signal
- started() signal
Now, I find a bit strange that started()
is after dataReady()
, because usually a process become in the running state before it can actually produce data, but I can not find anything about this order in the documentation, and maybe this is perfectly logical, but... Can someone explain me why this happens?
EDIT: I add here a code that behaves like I said. I am using Qt 4.8.4 64 bit.
/*****************************************************************************/
// multiproc.hh
#ifndef MULTIPROC_HH
#define MULTIPROC_HH
#include <QObject>
#include <QtCore>
#include <iostream>
#include "node.hh"
class MultiProc: public QObject {
Q_OBJECT
public:
MultiProc(QObject *parent = 0):
QObject(parent) {
std::cout << "MultiProc\n";
QList<QStringList> args;
args << (QStringList() << "/usr/bin/echo" << "Proc 0 running");
args << (QStringList() << "/usr/bin/echo" << "Proc 1 running");
args << (QStringList() << "/usr/bin/cat");
args << (QStringList() << "/usr/bin/cat");
args << (QStringList() << "/usr/bin/tee" << "/etc/hostname");
for (int i = 0; i < args.size(); ++i)
_nodes << new Node(this, i, args[i]);
}
signals:
void finished();
public slots:
void run() {
std::cout << "Starting all nodes :)\n";
foreach (Node *n, _nodes)
n->start();
}
private:
QList<Node *> _nodes;
};
#endif // MULTIPROC_HH
/*****************************************************************************/
// node.hh
#ifndef NODE_HH
#define NODE_HH
#include <QtCore>
#include <iostream>
class Node: public QObject {
Q_OBJECT
public:
Node(QObject *parent, int id, const QStringList &args):
QObject(parent),
_id(id),
_proc(new QProcess(this)),
_args(args) {
std::cout << "Node " << _id << " created with command "
<< args.join(" ").toStdString() << "\n";
connect(_proc, SIGNAL(started()), this, SLOT(started()));
connect(_proc, SIGNAL(finished(int)), this, SLOT(finished()));
connect(_proc, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(_proc, SIGNAL(error(QProcess::ProcessError)),
this, SLOT(error(QProcess::ProcessError)));
}
void start() {
if (_proc->state() == QProcess::NotRunning) {
std::cout << "Starting process on node: " << _id << "\n";
_proc->start(_args.at(0), _args.mid(1));
}
}
public slots:
void started() { std::cout << "Node " << _id << " started\n"; }
void finished() { std::cout << "Node " << _id << " finished\n"; }
void readyRead() { std::cout << "Node " << _id << " readyRead\n"; }
void error(QProcess::ProcessError err) {
std::cout << "Node " << _id << " Error: " << err << "\n";
QCoreApplication::exit(1);
}
private:
int _id;
QProcess *_proc;
QStringList _args;
};
#endif // NODE_HH
/*****************************************************************************/
// main.cpp
#include <QCoreApplication>
#include "multiproc.hh"
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
MultiProc *p = new MultiProc(&a);
QObject::connect(p, SIGNAL(finished()), &a, SLOT(quit()));
QTimer::singleShot(0, p, SLOT(run()));
return a.exec();
}
And here a piece of output:
Node 0 readyRead
Node 2 started
Node 0 started
Node 0 finished
Node 1 started
Node 1 readyRead
Node 4 started
Node 1 finished
Node 3 started