Geore Shg
Geore Shg

Reputation: 1329

QDesktopServices::openUrl with selecting specified file in explorer

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)

Upvotes: 5

Views: 2758

Answers (2)

ernie
ernie

Reputation: 16

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

Upvotes: 8

krlmlr
krlmlr

Reputation: 25474

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

Upvotes: 0

Related Questions