如何用Qt"在Finder中显示"或"在资源管理器中显示"

nnc*_*nnc 49 c++ qt qt4

是否可以在Windows资源管理器/ OS X Finder中打开文件夹,然后选择/突出显示该文件夹中的一个文件,并以跨平台方式执行此操作?现在,我做了类似的事情

QDesktopServices::openUrl( QUrl::fromLocalFile( path ) );
Run Code Online (Sandbox Code Playgroud)

哪里path是我要打开的文件夹的完整路径.显然,这只会打开文件夹,我将不得不手动追踪我需要的文件.当该文件夹中有数千个文件时,这有点问题.

如果我将它作为该文件夹中特定文件的路径,那么该文件是用该mime类型的默认应用程序打开的,这不是我需要的.相反,我需要相当于"在Finder中显示"或"在资源管理器中显示"的功能.

Ivo*_*Ivo 42

Qt Creator(源代码)具有此功能,复制它是微不足道的:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

另外,相关博客文章(使用简单的代码,我还没有尝试过,所以我无法评论),是这个.

编辑:

当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"
Run Code Online (Sandbox Code Playgroud)

从而,

QProcess::startDetached(explorer, QStringList(param));
Run Code Online (Sandbox Code Playgroud)

改为

QString command = explorer + " " + param;
QProcess::startDetached(command);
Run Code Online (Sandbox Code Playgroud)

  • 发布 GPL 代码并建议复制和粘贴?这是一个陷阱! (2认同)

lia*_*iaK 10

可能你可以QFileDialog::getOpenFileName用来获取文件名.该文档可在这里.以上函数将返回包括文件名和扩展名的完整路径 ,如果任何 ..

然后你可以给

QDesktopServices::openUrl(path);

以默认应用程序打开文件,其中pathQString返回QFileDialog::getOpenFileName.

希望能帮助到你..

  • 谢谢你的回答,但这不是我需要的.我更新了问题以试图澄清这一点.我需要的是"在Finder中显示"或"在资源管理器中显示"功能. (3认同)
  • 只是为了澄清(5 年后)...如果“路径”是一个目录,则 openUrl(path) 在默认文件资源管理器中打开该目录(它不会在常识中“显示”,这意味着打开父目录)。如果“path”是一个文件,它会使用默认应用程序打开。您可以检测路径是否为文件,然后删除文件名(仅保留目录),但不会在文件资源管理器中选择该文件。 (2认同)

Dan*_*edy 6

这是我根据之前答案的输入使用的代码。该版本不依赖于 Qt Creator 中的其他方法,接受文件或目录,并且具有错误处理和其他平台的回退模式:

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()));
}
Run Code Online (Sandbox Code Playgroud)

  • 对于 MacOS:请注意,“path”必须是绝对的。只需添加一条“return”指令即可抑制“osascript”的输出消息。 (2认同)

Eli*_*ias 5

在Windows资源管理器中打开文件(不是浏览器)

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

}
Run Code Online (Sandbox Code Playgroud)

  • 不跨平台。 (3认同)
  • 如果您使用的是 Windows 资源管理器附带的操作系统,那就太好了,但我的大多数用户更愿意避免使用这些操作系统。☺ (2认同)