Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    qprocess qt creator qt 6.3.0
    2
    4
    212
    Loading More Posts
    • 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
      BigBen last edited by BigBen

      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.

      JonB 1 Reply Last reply Reply Quote 0
      • JonB
        JonB @BigBen last edited by JonB

        @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 Reply Quote 3
        • B
          BigBen last edited by BigBen

          @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?

          JonB 1 Reply Last reply Reply Quote 0
          • JonB
            JonB @BigBen last edited by

            @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 Reply Quote 1
            • First post
              Last post