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. How to get output system command in Qt C++?
Forum Updated to NodeBB v4.3 + New Features

How to get output system command in Qt C++?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 4.6k Views 1 Watching
  • 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.
  • T Offline
    T Offline
    tadamo
    wrote on 8 Oct 2020, 20:12 last edited by
    #1

    Hello,

    I have a "test.exe" that I can use from the command line with the DOS console from Windows 10.
    Example:
    test.exe blue

    returns a return code of 1
    Otherwise, it returns 0.

    I would like to use this "test.exe" by using command line in C ++ Qt and get the output from this command line, but I searched on the internet and I can't find any simple code for it.
    For example, I found:

    QProcess process;
    process.start("test.exe blue");
    process.waitForFinished(-1); // will wait forever until finished
    
    QString stdout = process.readAllStandardOutput();
    QString stderr = process.readAllStandardError();
    

    But the last two lines are bad and don't work.

    Do you have a good example to use command line in C++ Qt and get the value of this command line, please?

    Thanks.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 8 Oct 2020, 20:16 last edited by
      #2

      Hi and welcome to devnet,

      You should add error checking. The application you are calling with QProcess may have not started properly or triggered an error.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • T Offline
        T Offline
        tadamo
        wrote on 8 Oct 2020, 20:24 last edited by
        #3

        Can you show me an example of code, please?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 8 Oct 2020, 20:26 last edited by
          #4

          @tadamo said in How to get output system command in Qt C++?:

          Can you show me an example of code, please?

          It's all in the doc: https://doc.qt.io/qt-5/qprocess.html#details

          And if you want the return code you should take a look at QProcess::exitCode()

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • T Offline
            T Offline
            tadamo
            wrote on 8 Oct 2020, 22:16 last edited by
            #5

            Thanks, but I can't get the value of exitcode. Here is my code:

            QString program = "test\\test.exe";
            QStringList arguments;
            arguments << "blue";
            
            QProcess *myProcess = new QProcess(this);
            myProcess->start(program, arguments);
            int res = myProcess->exitCode();
            

            I retrieve each time 0 in the varaible res whereas it should be 1.
            Could you fix my code please?

            Thanks.

            B 1 Reply Last reply 9 Oct 2020, 02:44
            0
            • T tadamo
              8 Oct 2020, 22:16

              Thanks, but I can't get the value of exitcode. Here is my code:

              QString program = "test\\test.exe";
              QStringList arguments;
              arguments << "blue";
              
              QProcess *myProcess = new QProcess(this);
              myProcess->start(program, arguments);
              int res = myProcess->exitCode();
              

              I retrieve each time 0 in the varaible res whereas it should be 1.
              Could you fix my code please?

              Thanks.

              B Offline
              B Offline
              Bonnie
              wrote on 9 Oct 2020, 02:44 last edited by Bonnie 10 Sept 2020, 02:49
              #6

              @tadamo
              You need to get the exit code AFTER the process finished.

              Asynchronous (non-blocking) way:

              QProcess *myProcess = new QProcess(this);
              connect(myProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this,
                   [=](int exitCode, QProcess::ExitStatus exitStatus){
                  //use exitCode here directly
              });
              myProcess->start(program, arguments);
              

              Synchronous (blocking) way:

              myProcess->start(program, arguments);
              myProcess->waitForFinished();
              int res = myProcess->exitCode();
              

              Note: you may also need to handle FailedToStart error when "start" or timeout when "waitForFinished"

              1 Reply Last reply
              5

              1/6

              8 Oct 2020, 20:12

              • Login

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