QDesktopServices::openUrl 在资源管理器中选择指定文件

发布于 2025-01-02 17:56:58 字数 117 浏览 2 评论 0原文

在大多数编码程序中,您可以右键单击该项目,然后单击“在资源管理器中显示”,它会在资源管理器中显示选定项目的文件。在 Qt 中使用 QDesktopServices 如何做到这一点? (或在 QT 中执行此操作的任何方式)

In most coding programs, you can right click on the item and click show in explorer and it shows the file in explorer with the item selected. How would you do that in Qt with QDesktopServices? (or any way to do it in QT)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

渔村楼浪 2025-01-09 17:56:58

您可以使用此方法在 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);

you can use this method to select file on Windows or MacOS ,if you want select on linux you can find a way in QtCreator sources.

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);
旧人哭 2025-01-09 17:56:58

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

PyQt4.QtGui.QDesktopServices.openUrl(PyQt4.QtCore.QUrl('file:///%s' % dirname))

Have you tried using the file:/// syntax? The following is taken from a code base I'm working with:

PyQt4.QtGui.QDesktopServices.openUrl(PyQt4.QtCore.QUrl('file:///%s' % dirname))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文