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)
Asked
Active
Viewed 2,566 times
2 Answers
6
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);

Pavel Strakhov
- 39,123
- 5
- 88
- 127

ernie
- 16
- 2
-
How's that better than one call of `openUrl`? – krlmlr Feb 04 '12 at 13:34
-
2Its better because it can select file and openurl can't – Kamil Klimek Feb 06 '12 at 11:31
-
I have found that the Q_OS_WIN solution doesn't work if there is a comma in the file path. using cmd, you can fix the issue surrounding the file path with quotes, but this solution doesn't work in qt. Any idea? – louissmr Jun 21 '14 at 11:49
0
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))

krlmlr
- 25,056
- 14
- 120
- 217
-
Yes, this is how to open a directory in a file manager window (such as Nemo or Windows Explorer). But the question was about selecting a file within that window. – Michael Scheper Jun 22 '16 at 09:45