Launch an executable from the same dir as a Qt app when white spaces in path

c++, executable, qprocess, qt

Solution

`execute(QString)` uses a single string for both executable path and arguments. Without proper quoting, `C:\A path with spaces\foo.exe` will be interpreted as `c:\A` as executable and `path`, `with` `spaces\foo.exe` as arguments.

To avoid this, use the overload `execute(QString, QStringList)` that takes the arguments as separate string list, even if you don't want to pass arguments at all:

QProcess::execute(path, QStringList());

This does the right thing and doesn't require any quoting from your side.

Problem

I would like to launch an executable when pushing a button in my Qt app. This `.exe` is always located in the same directory as the Qt app itself. Sometimes there are white spaces in the path to this directory. This seems to prevent the `.exe` from starting. Here is my code (that doesn't seem to work): ``` QString path = QCoreApplication::applicationDirPath (); path.append("/executable.exe"); QProcess process; process.execute(path); ``` I don't know if it is possible to start the `.exe` without showing a command prompt first. When the `.exe` is running I have to close the Qt app, while the `.exe` keeps running.

Original source