简体   繁体   中英

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!

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"));

ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM