63

Is it possible to open a folder in Windows Explorer/OS X Finder and then select/highlight one file in that folder, and do it in a cross platform way? Right now, I do something like

QDesktopServices::openUrl( QUrl::fromLocalFile( path ) );

where path is a full path to folder I want to open. Obviously, this will just open the folder, and I'll have to track down the file I need manually. This is a bit of a problem when there are thousands of files in that folder.

If I make it a path to specific file in that folder, then that file is open with default application for that mime type, and that is not what I need. Instead, I need the functionality equivalent to "Reveal in Finder" or "Show in Explorer".

3

6 Answers 6

49

Qt Creator (source) has this functionality, it's trivial to copy it:

void FileUtils::showInGraphicalShell(QWidget *parent, const QString &pathIn)
{
    const QFileInfo fileInfo(pathIn);
    // Mac, Windows support folder or file.
    if (HostOsInfo::isWindowsHost()) {
        const FileName explorer = Environment::systemEnvironment().searchInPath(QLatin1String("explorer.exe"));
        if (explorer.isEmpty()) {
            QMessageBox::warning(parent,
                                 QApplication::translate("Core::Internal",
                                                         "Launching Windows Explorer Failed"),
                                 QApplication::translate("Core::Internal",
                                                         "Could not find explorer.exe in path to launch Windows Explorer."));
            return;
        }
        QStringList param;
        if (!fileInfo.isDir())
            param += QLatin1String("/select,");
        param += QDir::toNativeSeparators(fileInfo.canonicalFilePath());
        QProcess::startDetached(explorer.toString(), param);
    } else if (HostOsInfo::isMacHost()) {
        QStringList scriptArgs;
        scriptArgs << QLatin1String("-e")
                   << QString::fromLatin1("tell application \"Finder\" to reveal POSIX file \"%1\"")
                                         .arg(fileInfo.canonicalFilePath());
        QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
        scriptArgs.clear();
        scriptArgs << QLatin1String("-e")
                   << QLatin1String("tell application \"Finder\" to activate");
        QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
    } else {
        // we cannot select a file here, because no file browser really supports it...
        const QString folder = fileInfo.isDir() ? fileInfo.absoluteFilePath() : fileInfo.filePath();
        const QString app = UnixUtils::fileBrowser(ICore::settings());
        QProcess browserProc;
        const QString browserArgs = UnixUtils::substituteFileBrowserParameters(app, folder);
        bool success = browserProc.startDetached(browserArgs);
        const QString error = QString::fromLocal8Bit(browserProc.readAllStandardError());
        success = success && error.isEmpty();
        if (!success)
            showGraphicalShellError(parent, app, error);
    }
}

Another, related blog post (with simpler code, I haven't tried it so I can't comment), is this.

Edit:

There is a bug in the original code when pathIn contains spaces on Windows. QProcess::startDetached will automatically quote a parameter if it contains spaces. However, Windows Explorer will not recognize a parameter wrapped in quotes, and will open the default location instead. Try it yourself in the Windows command line:

echo. > "C:\a file with space.txt"
:: The following works
C:\Windows\explorer.exe /select,C:\a file with space.txt  
:: The following does not work
C:\Windows\explorer.exe "/select,C:\a file with space.txt"

Thus,

QProcess::startDetached(explorer, QStringList(param));

is changed to

QString command = explorer + " " + param;
QProcess::startDetached(command);
11
  • 1
    I tested the code in the blog post you linked. It does work (at least on Mac OS X). Feb 14, 2011 at 10:23
  • 1
    @Michael Scheper: I ported DanDennedy's answer to PyQt5 (see here). I haven't tested it for Linux, but it is quite straightforward to look at.
    – normanius
    Feb 4, 2018 at 1:43
  • 1
    Your edit is incorrect. Explorer accepts a quote-wrapped path just fine (on Windows 10, at least). The key is that the explorer args must be comma-separated, so the original source code is correct when it uses /select, (with a comma) not /select. Qt will correctly wrap the path in quotes as necessary. Source: geoffchappell.com/studies/windows/shell/explorer/cmdline.htm
    – Phlucious
    Apr 25, 2019 at 21:13
  • 2
    Posting GPL code and suggest copy & paste? It's a trap! Oct 7, 2019 at 11:20
  • 1
    it it very exact about spaces part, even in Qt 5.15.0. QProcess::startDetached("explorer.exe", {"/select,c:\\space dir\\some.file"}); couldn't work; QProcess::startDetached("explorer.exe", {"/select,\"c:\\space dir\\some.file\""}); couldn't work; but deprecated QProcess::startDetached("explorer.exe /select,c:\\space dir\\some.file"}); works! And notice that there is no quotes arround the path. May 15, 2021 at 3:32
12

Probably you can use QFileDialog::getOpenFileName to get the file name. The documentation is available here.. The above function will return the complete path including the file name and its extension if any..

Then you can give

QDesktopServices::openUrl(path);

to open the the file in its default appication, where the path will be the QString returned by QFileDialog::getOpenFileName.

Hope it helps..

5
  • 3
    thanks for the answer, but that is not what I need. I updated the question to try to clarify that. What I need is "Reveal in Finder" or "Show in Explorer" functionality.
    – nnc
    Aug 16, 2010 at 4:51
  • The openUrl()-Method of QDesktopServices actually shows a file/directory in the explorer. Did you prepend "file:///" on your path string?
    – user925861
    Oct 15, 2014 at 8:50
  • 1
    @liaK: The OP just want the file show in the explorer, not open it. Your method will try to open the file itself.
    – diverger
    May 26, 2015 at 2:06
  • 3
    Just to clarify (5 years later)... if "path" is a directory, the openUrl(path) opens that directory in the default file explorer (it doesn't "reveal" in the common sense, which means to open the parent directory). If "path" is a file, it opens with the default application. You can detect if path is a file and then remove the filename (keeping just the directory), but it won't select the file in the file explorer.
    – cbuchart
    Jul 29, 2015 at 9:18
  • 1
    You don't have to prepend "file://" to the path string; there is QUrl::fromLocalFile(); you may have to use it with QFile#absoluteFilePath(), etc. if you're working with relative paths. cc @Stradivari
    – Jason C
    Dec 9, 2015 at 14:33
6

Here is the code that I went with based on the inputs from a previous answer. This version does not depend on other methods in Qt Creator, accepts a file or directory, and has a fall back mode for error handling and other platforms:

void Util::showInFolder(const QString& path)
{
    QFileInfo info(path);
#if defined(Q_OS_WIN)
    QStringList args;
    if (!info.isDir())
        args << "/select,";
    args << QDir::toNativeSeparators(path);
    if (QProcess::startDetached("explorer", args))
        return;
#elif defined(Q_OS_MAC)
    QStringList args;
    args << "-e";
    args << "tell application \"Finder\"";
    args << "-e";
    args << "activate";
    args << "-e";
    args << "select POSIX file \"" + path + "\"";
    args << "-e";
    args << "end tell";
    args << "-e";
    args << "return";
    if (!QProcess::execute("/usr/bin/osascript", args))
        return;
#endif
    QDesktopServices::openUrl(QUrl::fromLocalFile(info.isDir()? path : info.path()));
}
5
  • I like this approach because it's straightforward. I ported this to python/PyQt5, have a look here.
    – normanius
    Feb 4, 2018 at 1:36
  • 2
    As for MacOS: Note that path has to be absolute. One can suppress output messages from osascript by simply adding a return instruction.
    – normanius
    Feb 4, 2018 at 1:39
  • And one more thing: Just executing open dirPath would be even more straightforward on MacOS, but it does not highlight the file if path was actually a file.
    – normanius
    Feb 4, 2018 at 1:48
  • This should be the accepted answer. Thanks a lot.
    – Youda008
    Nov 7, 2021 at 12:47
  • Simple, short, and works without special things.
    – kluszon
    Oct 14, 2022 at 12:10
5

Open file in windows explorer (not browser)

void OpenFileInExplorer()
{
   QString path = "C:/exampleDir/example.txt";

   QStringList args;

   args << "/select," << QDir::toNativeSeparators(path);

   QProcess *process = new QProcess(this);
   process->start("explorer.exe", args); 

}
4
  • 2
    Nice if you're using an operating system that comes with Windows Explorer, but most of my users prefer to avoid those. ☺ Jun 22, 2016 at 9:50
  • Little bit of a Windows-specific hack but works great, thanks!
    – mrexodia
    Oct 7, 2016 at 17:43
  • 3
    Not cross platform.
    – ZN13
    Jul 8, 2018 at 17:54
  • You didn't provide the third argument needed by the start function & also this is not cross platform solution Mar 15, 2021 at 20:56
3

This solution works on both Windows and Mac:

void showFileInFolder(const QString &path){
    #ifdef _WIN32    //Code for Windows
        QProcess::startDetached("explorer.exe", {"/select,", QDir::toNativeSeparators(path)});
    #elif defined(__APPLE__)    //Code for Mac
        QProcess::execute("/usr/bin/osascript", {"-e", "tell application \"Finder\" to reveal POSIX file \"" + path + "\""});
        QProcess::execute("/usr/bin/osascript", {"-e", "tell application \"Finder\" to activate"});
    #endif
}
0
0

Here is a Linux option similar to Dan Dennedy's answer, which utilizes DBus to use the default file manager:

#elif defined(Q_OS_LINUX)
    QStringList args;
    args << "--session";
    args << "--dest=org.freedesktop.FileManager1";
    args << "--type=method_call";
    args << "/org/freedesktop/FileManager1";
    args << "org.freedesktop.FileManager1.ShowItems";
    args << "array:string:file://" + path;
    args << "string:";
    if (QProcess::startDetached("dbus-send", args))
        return;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.