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. QProcess not working in QT Creator (using qt version 6.3.0)

QProcess not working in QT Creator (using qt version 6.3.0)

Scheduled Pinned Locked Moved Solved General and Desktop
qprocessqt creatorqt 6.3.0
4 Posts 2 Posters 1.5k 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.
  • B Offline
    B Offline
    BigBen
    wrote on 9 Jun 2022, 10:31 last edited by BigBen 6 Sept 2022, 10:31
    #1

    Hi.

    I am trying to run a simple QProcess task in a simple application written on QT Creator.

    The code is as follows:

    void Structure::on_button1_clicked()
    {
        p = new QProcess(this);
        p->start("dir C:/Users");
        p->waitForFinished();
        QString output = p->readAllStandardOutput();
        qDebug<<output;
    }
    

    However, the output I get is an empty string, even though the Users directory has a number of files and folders.
    I don't know what could be going wrong in such a simple process.

    I have already included the QProcess header.

    J 1 Reply Last reply 9 Jun 2022, 10:45
    0
    • B BigBen
      9 Jun 2022, 10:31

      Hi.

      I am trying to run a simple QProcess task in a simple application written on QT Creator.

      The code is as follows:

      void Structure::on_button1_clicked()
      {
          p = new QProcess(this);
          p->start("dir C:/Users");
          p->waitForFinished();
          QString output = p->readAllStandardOutput();
          qDebug<<output;
      }
      

      However, the output I get is an empty string, even though the Users directory has a number of files and folders.
      I don't know what could be going wrong in such a simple process.

      I have already included the QProcess header.

      J Offline
      J Offline
      JonB
      wrote on 9 Jun 2022, 10:45 last edited by JonB 6 Sept 2022, 11:04
      #2

      @BigBen

      • QProcess::start(const QString &program, const QStringList &arguments = {}, QIODeviceBase::OpenMode mode = ReadWrite) does not accept a complete command line, only a single executable as its first argument. Change over to the correct new overload.

      • QProcess::readAllStandardOutput() is not going to read an error message. You must call QProcess::readAllStandardError() too. You also don't attach to signal QProcess::errorOccurred(). I think what you have will be generating one of these, possibly the latter.

      • dir is not an executable program under Windows. It is built into cmd.exe, which you must run. (Unless Qt handles this for you, I don't have Qt under Windows.)

      • dir C:/Users is wrong/lucky if it works under Windows. Use proper native \s for paths in QProcess commands, it does not know an argument is a path and will not translate /s to \s for you, unlike e.g. QFile.

      p->start("cmd", { "/c", "dir C:\\Users" });
      p->waitForFinished();
      QString output = p->readAllStandardOutput();
      QString error = p->readAllStandardError();
      

      UPDATE
      At Qt 6.0 it seems they introduced void QProcess::startCommand(const QString &command, QIODeviceBase::OpenMode mode = ReadWrite). So this would probably also work:

      p->startCommand("dir C:\\Users")
      
      1 Reply Last reply
      3
      • B Offline
        B Offline
        BigBen
        wrote on 10 Jun 2022, 02:50 last edited by BigBen 6 Oct 2022, 02:51
        #3

        @JonB
        Hi. Thank you for the explanation. The command p->start("cmd", { "/c", "dir C:\\Users" }); gives me the expected output.

        However, p->startCommand("dir C:\\Users") gives me no error and no output.

        I want to ask that in p->start("cmd", { "/c", "dir C:\\Users" });, what does the /c mean in the second argument?

        J 1 Reply Last reply 10 Jun 2022, 07:18
        0
        • B BigBen
          10 Jun 2022, 02:50

          @JonB
          Hi. Thank you for the explanation. The command p->start("cmd", { "/c", "dir C:\\Users" }); gives me the expected output.

          However, p->startCommand("dir C:\\Users") gives me no error and no output.

          I want to ask that in p->start("cmd", { "/c", "dir C:\\Users" });, what does the /c mean in the second argument?

          J Offline
          J Offline
          JonB
          wrote on 10 Jun 2022, 07:18 last edited by
          #4

          @BigBen said in QProcess not working in QT Creator (using qt version 6.3.0):

          However, p->startCommand("dir C:\\Users") gives me no error and no output.

          Well that is interesting. As I said I only just discovered looking at the docs. It's new in Qt 6.0. I do not use Qt 6, nor Windows, so I cannot comment. If you look at he examples at https://doc.qt.io/qt-6/qprocess.html#startCommand you can see them claiming to issue dir commands that way, that's all I can say.

          I want to ask that in p->start("cmd", { "/c", "dir C:\\Users" });, what does the /c mean in the second argument?

          The Windows/DOS cmd.exe takes various arguments, as you can see from running cmd /?. Including:

          /C      Carries out the command specified by string and then terminates
          

          Basically without the /c it runs an interactive Command Prompt, the command-line console. With the /c it interprets and executes the command line string following it, i.e. your dir .... It does not open a Command Prompt window and it returns to your code immediately after executing the command. dir is built into cmd.exe --- along with many other "simple" commands like echo or mkdir --- there is no external dir.exe file to execute, so we need cmd /c dir to perform it.

          1 Reply Last reply
          1

          2/4

          9 Jun 2022, 10:45

          • Login

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