run Macos terminal command from QT
-
wrote on 12 Jul 2015, 10:51 last edited by
I try to execute command but there are no result in output... :( I try
QStringList arguments;
arguments << "";
QProcess exec;
exec.start("time", arguments);
exec.waitForFinished();
qDebug() << exec.readAllStandardOutput();And I try
QStringList arguments; arguments <<"time"; QProcess exec; exec.start("/bin/sh", arguments); exec.waitForFinished(); qDebug() << exec.readAllStandardOutput();
What I am doing wrong?
-
wrote on 12 Jul 2015, 11:25 last edited by
The output of time is rather short IIRC. This might be a buffer problem. Try to use a commend with considerably more output and check if this is shown.
At least this tells you if you have a fundamental problem. -
did you try with full path for time ?
/usr/bin/time or where it is on your distroAlso if you run it by hand as "/bin/sh time" does that produce the result you expect?
-
wrote on 12 Jul 2015, 11:52 last edited by
Is not work
-
@sashapont
I think your issue is the sh shell.if you read
http://stackoverflow.com/questions/10701504/command-working-in-terminal-but-not-via-qprocessit seems you need "-c" to make SH run the time command as its external.
QStringList arguments; arguments << "-c" << "time" ; QProcess exec; exec.start("/bin/sh", arguments); exec.waitForFinished(); qDebug() << exec.readAllStandardOutput();
-
note
it seems that "time" displays on stderror so you need to read from readAllStandardError and not readAllStandardOutput
Below code display output of time on ubuntu box.
#include <QStringList> #include <QProcess> #include <QDebug> #include <QByteArray> int main(int argc, char *argv[]) { QProcess exec; QStringList arguments; arguments << "-c" << "time" ; exec.start("/bin/sh", arguments); int res=exec.waitForStarted(); if (! res ) qDebug() << exec.errorString(); exec.waitForFinished(); QByteArray outData1 = exec.readAllStandardOutput(); qDebug()<<QString(outData1); QByteArray outData2 = exec.readAllStandardError(); qDebug()<<QString(outData2); }
4/6