How to "Reveal in Finder" or "Show in Explorer" with Qt

QT: Launch default exporer on a directory selecting a file

Ok, I want to thank @Chernobyl for his useful help.

However I've found a general solution posted here:
How to "Reveal in Finder" or "Show in Explorer" with Qt

It wasn't compiling at first because the QtCreator code is using a class called Environment which I tried to add to my project but that file then also includes others, etc. I checked and they are using it just to see if explorer.exe can be found on the system path. It was not so important (to me) so I removed that check and then I tested it. It's working perfectly. It's also supposed to work for Mac and Linux.

Opening files from Finder with a Qt-based application?

QFileOpenEvent (Qt4/Qt5) should do the trick.

Also see https://doc.qt.io/archives/qq/qq18-macfeatures.html

Does Qt support 'Open a folder and highlight a particular file'?

No. What you are trying to do is platform-specific (i.e. instruct the native file browser to open and perform some specific action) and therefore not supported.

Qt does have platform-specific features, but they largely are focused on enabling platform-specific interaction (like getting a native OS X menu handle) rather than integrating platform-specific behavior. Yes, pretty much all platforms have some form of file browser, but they also have lots of APIs, quirks, and features not available elsewhere. Qt does its best to balance being feature-rich without binding too tightly to the platform.

The closest you can come is with QDesktopServices::openUrl, but that just opens the directory. Check out QProcess::execute to call the native file browser along with command-line arguments.

Is there a native file explorer in Qt that I can use to have the user select path to a specific file?

This is what QFileDialog is trying to achieve, so I would suggest to use that if it is a widget based application. All you will need to write is something like this:

fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"), "/home/jana", tr("Image Files (*.png *.jpg *.bmp)"));

This will bring up a dialog for selection that the user can use for navigation.

If you happen to use QML, you could give a try to the FileDialog component. Then, you would write something like this:

import QtQuick 2.2
import QtQuick.Dialogs 1.0

FileDialog {
id: fileDialog
title: "Please choose a file"
onAccepted: {
console.log("You chose: " + fileDialog.fileUrls)
Qt.quit()
}
onRejected: {
console.log("Canceled")
Qt.quit()
}
Component.onCompleted: visible = true
}

QDesktopServices::openUrl with selecting specified file in explorer

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

How to open a file explorer from Qt?

Try QProcess

#include <QProcess>

int main(int argc, char *argv[])
{
QProcess::startDetached("C:\\Windows\\explorer.exe", {});
return 0;
}


Related Topics



Leave a reply



Submit