PDA

View Full Version : Qt5/C++ - QProcess, capture terminal data



jimbo
31st December 2015, 18:50
Qt5.5.1
QtCreator 3.5.1
Windows 7

Hello,

I'm runnning a QProcess, and it runs fine. (7z, command line version of 7Zip)
The command I'm running outputs to the command prompt console continuously changing the % value
of the un-zipping progress. Run from the command prompt, shows:-
15% - myFile.zip
What I can't work out is how to redirect the console to my program, so I can show a progress bar.
I underatand the QPogressBar procedures.
Thanks for any advice offered.

Regards
process->setProcessChannelMode(QProcess::ForwardedChannels) ;
process->start(cmd);
do {
delay(1000); //processes events
qDebug() << "o - " << (process->readAllStandardOutput());
qDebug() << "e - " << (process->readAllStandardError());
} while(busy); //flag, changes when QProcess finishes, signal connected to slot
process->waitForFinished(-1); // -1, forever
process->close();Output:-
o - ""
e - ""
o - ""
e - ""
...
...

anda_skoa
31st December 2015, 18:55
Sometimes shell programs don't actually write to the output streams but to the terminal directly.

In such cases they often have command line options to specifically enable writing to the streams.

Cheers,
_

jimbo
1st January 2016, 12:47
Hello anda_skoa,

Thanks for your reply.
I've added some options to the called program and reduced my QProcess to this:-

process->setProcessChannelMode(QProcess::ForwardedChannels) ;
process->start(cmd);
do {
delay(500); //process events
} while(busy);
process->waitForFinished(-1);
process->close();
Application output tab, when the program is run inside QtCreator:-

0%


0% - myData.img


1% - myData.img


2% - myData.img
...
...Of course, If I take out the first program line there is no output.
Is there any way I can capture the output from 'process->setProcessChannelMode(QProcess::ForwardedChannels) '.

Regards

anda_skoa
1st January 2016, 14:01
If you don't forward the channels but connect slots to the two readyRead signals, do these slots get invoked?

Cheers,
_

jimbo
1st January 2016, 15:03
Hello anda_skoa.

Thanks for your reply and help.
'readyReadStandardOutput'. No help.
'readyReadStandardError'. Does help.
I can get the '%' from this.

Regards

process = new QProcess(this);
QObject::connect(process, SIGNAL(started()), this, SLOT(processStarted()));
QObject::connect(process, SIGNAL(readyReadStandardOutput()),this,SLOT(readyR eadStandardOutput()));
QObject::connect(process, SIGNAL(readyReadStandardError()),this,SLOT(readyRe adStandardError()));
QObject::connect(process, SIGNAL(finished(int)), this, SLOT(processFinished()));

void MainWindow::readyReadStandardError() //slot
{
QString errorString;
errorString.append(process->readAllStandardError());
qDebug() << "readyReadStandardError - " << errorString;
}

SLOT 'readyReadStandardError' gives:-
readyReadStandardError - " 0%"
readyReadStandardError - "\r \r 0% - myData.img"
readyReadStandardError - "\r \r 1% - myData.img"
readyReadStandardError - "\r \r 2% - myData.img"
readyReadStandardError - "\r \r 3% - myData.img"
...
...