如何使用 Qt “在 Finder 中显示”或“在资源管理器中显示”

新手上路,请多包涵

是否可以在 Windows Explorer/OS X Finder 中打开一个文件夹,然后选择/突出显示该文件夹中的一个文件,并以跨平台方式进行?现在,我做类似的事情

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

其中 path 是我要打开的文件夹的完整路径。显然,这只会打开文件夹,我必须手动找到我需要的文件。当该文件夹中有数千个文件时,这有点问题。

如果我将其设为该文件夹中特定文件的路径,则该文件将使用该 mime 类型的默认应用程序打开,这不是我所需要的。相反,我需要与“在 Finder 中显示”或“在资源管理器中显示”等效的功能。

原文由 nnc 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 970
1 个回答

Qt Creator ( source ) 有这个功能,复制它很简单:

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

另一个相关的博客文章(代码更简单,我没有尝试过, 所以 无法发表评论),是 .

编辑:

当 pathIn 在 Windows 上包含空格时,原始代码中存在错误。如果参数包含空格, QProcess::startDetached 将自动引用它。但是,Windows 资源管理器将无法识别用引号括起来的参数,而是打开默认位置。在 Windows 命令行中自己尝试一下:

 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"

因此,

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

改为

QString command = explorer + " " + param;
QProcess::startDetached(command);

原文由 Ivo 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题