Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. run Macos terminal command from QT

run Macos terminal command from QT

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sashapont
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      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.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        1
        • S Offline
          S Offline
          sashapont
          wrote on last edited by
          #4

          Is not work

          mrjjM 1 Reply Last reply
          0
          • S sashapont

            Is not work

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @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();
            
            1 Reply Last reply
            1
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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);
               
              }
              
              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved