QProcess::readAll and QProcess::readAllStandardOutput both return an empty string after QProcess::write() is run
-
@VRonin said in QProcess::readAll and QProcess::readAllStandardOutput both return an empty string after QProcess::write() is run:
hi
Thank you very much for the solution!
Just one small issue:
error: ‘connect’ was not declared in this scope connect(p,&QProcess::finished,&looper,&QEventLoop::quit); ^
Not so sure about this one...
-
@VRonin hi, same 'same' error
My includes for what it is worth
#include <QCoreApplication> #include <QLoggingCategory> #include <QTextStream> #include <QProcess> #include <QString> #include <QVariant> #include <QDebug> #include <QObject> #include <QEventLoop>
Edited Code as give above:
QString runCommand(const QString& cmd, const QString& input){ //I ignore cmd for testing purposes. I use a straightup cd to script and execute QString result; QEventLoop looper; QProcess *p = new QProcess(&looper); p->setProcessChannelMode(QProcess::MergedChannels); QObject::connect(p,&QProcess::finished,&looper,&QEventLoop::quit); QObject::connect(p,&QProcess::errorOccurred,[&result]()->void{qDebug("Error in Process"); result.clear();}); QObject::connect(p,&QProcess::errorOccurred,&looper,&QEventLoop::quit); QObject::connect(p,&QProcess::started,[p,&input]()->void{p->write((input +'\n').toLatin1());}); QObject::connect(p,&QProcess::readyReadStandardOutput,[p,&result]()->void{result+=p->readAllStandardOutput();}); const QString c = QString("sh -c \" cd /home/dev; ./script\" "); p->start(c); looper.exec(); return result; }
Error:
main.cpp:296: error: no matching function for call to ‘QObject::connect(QProcess*&, <unresolved overloaded function type>, QEventLoop*, void (QEventLoop::*)())’ QObject::connect(p,&QProcess::finished,&looper,&QEventLoop::quit); ^
-
My bad. if it happens again look at the documentation for the signal and/or slot (depending where
<unresolved overloaded function type>
appears). In this case: http://doc.qt.io/qt-5/qprocess.html#finishedso instead of
&QProcess::finished
usestatic_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished)
-
I would like to post my Stack Oveflow answer. It's actually an example where we write the name and get also the output given by the script.
Code:
#include <QCoreApplication> #include <QProcess> #include <QDebug> class MyProcess : public QProcess { Q_OBJECT public: MyProcess(QObject *parent = 0); ~MyProcess() {} public slots: void myReadyRead(); void myReadyReadStandardOutput(); }; MyProcess::MyProcess(QObject *parent) { connect(this,SIGNAL(readyRead()), this,SLOT(myReadyRead())); connect(this,SIGNAL(readyReadStandardOutput()), this,SLOT(myReadyReadStandardOutput())); } void MyProcess::myReadyRead() { qDebug() << Q_FUNC_INFO; } void MyProcess::myReadyReadStandardOutput() { qDebug() << Q_FUNC_INFO; // Note we need to add \n (it's like pressing enter key) this->write(QString("myname" + QString("\n")).toLatin1()); // Next line no required // qDebug() << this->readAll(); qDebug() << this->readAllStandardOutput(); } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); MyProcess *myProcess = new MyProcess(); QString program = "/home/fran/code/myscript.sh"; myProcess->start("/bin/sh", QStringList() << program); a.exec(); } #include "main.moc"
Script:
echo "enter your name:" read n if [ ! -z "$n" ]; then echo "success" exit 0; else echo "failed" exit 1; fi
-
@tarod-net
Hi!
I have this output without input stream from keyboard:void MyProcess::myReadyRead() void MyProcess::myReadyReadStandardOutput() "enter your name\n" void MyProcess::myReadyRead() void MyProcess::myReadyReadStandardOutput() "success\n"
Is this correct?
-
QProcess has quite extensive support for synchronous use.
#include <QCoreApplication> #include <QDebug> #include <QProcess> int main(int argc, char **argv) { QCoreApplication app(argc, argv); QProcess process; process.start("ls", {"-l"}); if (process.waitForFinished()) { qDebug() << "returned:" << process.readAllStandardOutput(); } else { qDebug() << process.errorString(); } process.start("ls", {"-l"}); while (process.waitForReadyRead()) { while (process.canReadLine()) qDebug() << process.readLine().trimmed(); } }