reading and writing to QProcess in Qt Console Application
-
Noted: this appears to be a specific issue question but hopefully it can be edited for all to related to
I need to interact with a QProcess object.
The Problem:
I am not getting any output from
QProcess
after callingQProcess:write(input)
More Info:
Going through the doc pages led me to create an example below:
I have a script requesting user input, and finally displaying and appropriate message based on the user input.
Testing:
After adding a "log" feature to my script for testing, the following occurs:
- script executes
- script requests user input (confirmed by the 'first'
qDebug() << p->readAll()
) - script accepts input from
QProcess
(confirmed by script 'log output')
After this, no output is received. The following 2 debug statements both fire (i.e. wait 30s each)
if (!p->waitForReadyRead()) { qDebug() << "waitForReadyRead() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString(); } if (!p->waitForFinished()) { qDebug() << "waitForFinished() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString(); }
Followed by:
QString s = QString(p->readAll() + p->readAllStandardOutput());
where
s
is an empty string.The issue is
s
should contain either "success" or "failed"Calling Code:
QString cmd = QString("sh -c \"/path/to/bashscript.sh\""); QString input = QString("Name"); QString result = runCommand(cmd, input)
Process Code:
//takes 2 parameters, // cmd which is the code to be executed by the shell // input which acts as the user input QString runCommand(QString cmd, QString input){ QProcess *p = new QProcess(new QObject()); p->setProcessChannelMode(QProcess::MergedChannels); //no actual reason to do this p->start(cmd); if (p->waitForStarted()) { if (!p->waitForReadyRead()) { qDebug() << "waitForReadyRead() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString(); } if (!p->waitForFinished()) { //reads current stdout, this will show the input request from the bash script //e.g. please enter your name: qDebug() << p->readAll(); //here I write the input (the name) to the process, which is received by the script p->write(ps.toLatin1()); //the script should then display a message i.e. ("success" o "failed") if (!p->waitForReadyRead()) { qDebug() << "waitForReadyRead() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString(); } if (!p->waitForFinished()) { qDebug() << "waitForFinished() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString(); } } QString s = QString(p->readAll() + p->readAllStandardOutput()); return s; } else{ qDebug() << "waitForStarted() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString(); } p->waitForFinished(); p->kill(); return QString(); }
script.sh (
-rwxrwxr-x
)#!/bin/bash #returns # "success" on non empty $n value # "failed: on empty $n value # echo "enter your name:" read n if [[ ! -z $n ]]; then echo "success" exit 0; else echo "failed" exit 1; fi
UPDATE
I modified the run command to use the QStringList with the args.
Still does not get output, infact the
waitForReadyRead()
andwaitForFinished()
returns false
instantly.Called with:
QString r = runCommand(QString("text"));
Process Code:
QString runCommand(QString input){ QProcess *p = new QProcess(new QObject()); p->setProcessChannelMode(QProcess::MergedChannels); //script is the same script refered to earlier, and the `cd /home/dev` IS required p->start("sh", QStringList() << "-c" << "cd /home/dev" << "./script"); ; if (p->waitForStarted()) { if (!p->waitForReadyRead(5000)) { qDebug() << "waitForReadyRead() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString(); } qDebug() << p->readAll(); p->write(input.toLatin1()); if(!p->waitForFinished(5000)){ qDebug() << "waitForFinished() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString(); } QString s = QString(p->readAll() + p->readAllStandardOutput()); return s; } else{ qDebug() << "waitForStarted() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString(); } p->waitForFinished(); p->kill(); return QString(); }
Terminal Output of the Process:
started readChannelFinished exit code = "0" waitForReadyRead() [false] : CODE: "5" | ERROR STRING: "Unknown error" "" waitForFinished() [false] : CODE: "5" | ERROR STRING: "Unknown error" Press <RETURN> to close this window...
Thoughts on this?
-
@CybeX You already asked more or less the same question in another thread. In that another thread you was tolled to use the assynchronous API (there was even code for you). You should not try to do it in synchronous way, you are working against Qt. Use signals and slots.
https://forum.qt.io/topic/75454/qprocess-readall-and-qprocess-readallstandardoutput-both-return-an-empty-string-after-qprocess-write-is-run/6