0

My application displays a grid of images. When the right mouse button is clicked a context menu is displayed where the first option is "Show in Explorer".

I was able to launch the default explorer on the directory by using:

QDesktopServices::openUrl(QUrl::fromLocalFile( directory ));

However I want also the file to be selected.

Is this possible? I suppose it should be, as many apps can do that.

Stephen H. Anderson
  • 978
  • 4
  • 18
  • 45

2 Answers2

1

openUrl is not suitable here. You need just start another process (with QProcess start or startDetached) with Windows explorer special arguments:

explorer.exe /select,"C:\pathTo\file.txt"

Jablonski
  • 18,083
  • 2
  • 46
  • 47
  • Thanks, but what happens if the user is on a Mac or Linux? – Stephen H. Anderson Jul 25 '15 at 16:38
  • @StephenH.Anderson I thought that you need Windows approach. Probably there is no easy cross-platform way to do this. On Windows it is explorer, on ubuntu it can be dolphin or nautilus or something else. – Jablonski Jul 25 '15 at 16:47
  • Ok Chernobyl, thanks, Will research for Mac specially what I need to do. – Stephen H. Anderson Jul 25 '15 at 16:49
  • BTW, I'm not able to make it work on windows yet. I've used this: QProcess::startDetached("explorer.exe", QStringList() << "/select" << "\"" + filePath + "\""); but it's opening the My Documents folder. – Stephen H. Anderson Jul 25 '15 at 16:49
  • @StephenH.Anderson https://support.microsoft.com/en-us/kb/152457 it seems that you forgot about comma , Also I didn't work with Mac so I coouldn't find same command for Finder. – Jablonski Jul 25 '15 at 16:59
0

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.

Community
  • 1
  • 1
Stephen H. Anderson
  • 978
  • 4
  • 18
  • 45