aardvarkk
aardvarkk

Reputation: 15996

Qt: QProcess result doesn't match result at prompt

I'm trying to execute the following command from Qt:

explorer /select,C:\Temp Folder\temp.wav

This should show the file temp.wav as selected in an Explorer window. It works correctly when run from the command prompt.

However, when I try the following in Qt:

QProcess::startDetached(
            "explorer",
            QStringList("/select,C:\\Temp Folder\\temp.wav")
            );

it doesn't work -- it opens Explorer but puts me in the "My Documents" folder. If I rename the folder to one without a space (TempFolder), it works correctly.

I've tried escaping the space in the folder name, placing quotes around the entire path, and many other combinations without success. Many combinations work correctly in cmd but do not seem to work when called through QProcess::startDetached.

The most confusing part is that the code I'm trying to copy is from QtCreator source code where they use something similar to open up a file in the Explorer window. Theirs successfully opens files with spaces in the path, but I just can't seem to recreate it!

Upvotes: 1

Views: 180

Answers (1)

vahancho
vahancho

Reputation: 21258

When you make such call:

QProcess::startDetached("explorer",
                        QStringList("/select,C:\\Program Files\\7-Zip\\7z.exe"));

Qt transforms the argument string into the:

explorer "/select,C:\Program Files\7-Zip\7z.exe"

which is not a valid option to open Explorer and select the given file. This happens, because your single argument has space(s) and Qt escapes it with quotes. To fix this problem you need to make the following call:

QProcess::startDetached("explorer",
           (QStringList() << "/select," << "C:\\Program Files\\7-zip\\7z.exe"));

i.e. pass two arguments. This will produce the following string:

explorer /select, "C:\Program Files\7-Zip\7z.exe"

which is valid and will do what is intended.

Upvotes: 3

Related Questions