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?



  • 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.


  • Qt Champions 2017

    did you try with full path for time ?
    /usr/bin/time or where it is on your distro

    Also if you run it by hand as "/bin/sh time" does that produce the result you expect?



  • Is not work


  • Qt Champions 2017

    @sashapont
    I think your issue is the sh shell.

    if you read
    http://stackoverflow.com/questions/10701504/command-working-in-terminal-but-not-via-qprocess

    it 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();
    

  • Qt Champions 2017

    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);
     
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.