4

在大多数编码程序中,您可以右键单击项目并单击在资源管理器中显示,它会在资源管理器中显示文件并选择项目。您将如何在 Qt 中使用 QDesktopServices 做到这一点?(或在 QT 中进行的任何方式)

4

2 回答 2

6

您可以使用此方法在 Windows 或 MacOS 上选择文件,如果您想在 linux 上选择文件,您可以在 QtCreator 源代码中找到方法。

void select(const QString& path){
#if defined(Q_OS_WIN)
    const QString explorer = "explorer";
        QStringList param;
        if (!QFileInfo(path).isDir())
            param << QLatin1String("/select,");
        param << QDir::toNativeSeparators(path);
        QProcess::startDetached(explorer, param);
#elif defined(Q_OS_MAC)
    QStringList scriptArgs;
        scriptArgs << QLatin1String("-e")
                   << QString::fromLatin1("tell application \"Finder\" to reveal POSIX file \"%1\"")
                                         .arg(path);
        QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
        scriptArgs.clear();
        scriptArgs << QLatin1String("-e")
                   << QLatin1String("tell application \"Finder\" to activate");
        QProcess::execute("/usr/bin/osascript", scriptArgs);
于 2012-02-04T04:32:23.033 回答
0

您是否尝试过使用file:///语法?以下内容来自我正在使用的代码库:

PyQt4.QtGui.QDesktopServices.openUrl(PyQt4.QtCore.QUrl('file:///%s' % dirname))
于 2012-02-04T01:43:56.597 回答