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 12 Jul 2015, 10:51 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 12 Jul 2015, 11:25 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
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 12 Jul 2015, 11:27 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 12 Jul 2015, 11:52 last edited by
          #4

          Is not work

          M 1 Reply Last reply 12 Jul 2015, 19:40
          0
          • S sashapont
            12 Jul 2015, 11:52

            Is not work

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 12 Jul 2015, 19:40 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
            • M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 12 Jul 2015, 20:52 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

              3/6

              12 Jul 2015, 11:27

              • Login

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