问题
I've found similar questions but never the exact answer. I have Qt program that starts a QProcess and writes the output to a QTextEdit box, so far so good. But it only does so when the program has ended. if possible I'd like the programs stdout to be printed in real-ish time. In an ideal world there would be some sort of signal that QProcess emits when there is a line ready to read, if its not possible with QProcess is it possible at all? Also Ideally you could still use the rest of the program while the process is running.
Heres some of the code i have so far, very simple, it just emits the first line of the QProcess stdout to a QTextEdit
...
extProcess::extProcess(QObject *parent) :
QObject(parent)
extProcess::extProcess(QObject *parent) :
QObject(parent)
{
proc = new QProcess(this); //initialize proc
arguments << "-v";
connect(proc, SIGNAL(readyRead()), this, SLOT(logReady()));
}
void extProcess::startProcess()
{
emit clearLog();
emit outLog("--Process started--");
proc->start("/Users/jonathan/Desktop/testgg");
}
void extProcess::logReady()
{
emit outLog(proc->readLine());
}
...
This is al alternate version I tried, this will show the entire QProcess output but still only shows it when the program finishes.
...
extProcess::extProcess(QObject *parent) :
QObject(parent)
{
proc = new QProcess(this); //initialize proc
proc->setProcessChannelMode(QProcess::SeparateChannels);
arguments << "-v";
connect(proc, SIGNAL(readyReadStandardOutput()), this, SLOT(logReady()));
}
void extProcess::logReady()
{
while(proc->bytesAvailable()){
emit outLog(proc->readLine());
}
}
void extProcess::startProcess()
{
emit clearLog();
emit outLog("--Process started--");
proc->start("/Users/jonathan/Desktop/testgg");
}
void extProcess::killProcess()
{
proc->terminate();
emit clearLog();
emit outLog("--Process Terminated--");
}
....
Thanks
回答1:
I use readAllStandardOutput() for this exact purpose and it works for me.
However I did note that it won't recieve any standard output until the process actually flushes its output buffer ("\n" may not automagically do this, at least not in my entirely platform specific Windows experience).
Depending on how the child process writes its output (either a C app or a C++ app), it needs to call fflush(stdout); or end lines with std::endl; respectively.
回答2:
readLine() probably waits till the first '\n' character is read.
readAllStandardOutput on the other hand returns all data available from the standard output of the process.
So it seems to me that if you use readAllStandardOutput() you may get more performance.
You have to try though.
回答3:
In an ideal world there would be some sort of signal that QProcess emits when there is a line ready to read
Is the Qprocess signal readyReadStandardOutput() not exactly what your asking for ?
You can connect to it then is your slot get the available data bytesAvailable() and look for the '/n' chars. Or simply readLine() while canReadLine().
Do not forget to start your process and don't wait for it to finish. Plus set-up the ProcessChannelMode to SeparateChannels.
EDIT
I do have a class that read line of a QProcess like this :
bool extProcess::logReady()
{
while( proc->canReadLine())
{
QByteArray line = proc->readLine();
/*do some with the line like copying it's content to a QTextEdit*/
}
}
And it work quite well. I not sure about the emit outLog() signal ! Are you try to pass a QByteArray through a signal ? The data contained in the QByteArray must be save in a buffer and you should pass the pointer to this array to anyone connected to it ...
But again if you don't want to use extra memory and deal with the "is the data consumed ? ... ", do what you need to do with the data in your logReady() method.
来源:https://stackoverflow.com/questions/6279182/qt-is-there-a-way-to-send-a-signal-when-a-qprocess-writes-a-line-to-stdout