QProcess not working in QT Creator (using qt version 6.3.0)
-
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.
-
-
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 callQProcess::readAllStandardError()
too. You also don't attach to signalQProcess::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 intocmd.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 inQProcess
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")
-
-
@JonB
Hi. Thank you for the explanation. The commandp->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? -
@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 runningcmd /?
. 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. yourdir ...
. It does not open a Command Prompt window and it returns to your code immediately after executing the command.dir
is built intocmd.exe
--- along with many other "simple" commands likeecho
ormkdir
--- there is no externaldir.exe
file to execute, so we needcmd /c dir
to perform it.