run Macos terminal command from QT
-
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?
-
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?
-
@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); }