17

I have the following:

QProcess *process = new QProcess(this);
QString path = QDir::toNativeSeparators(QApplication::applicationPath);
#if defined(Q_OS_WIN)

process->start("explorer.exe",  QStringList() << path);

#elif defined(Q_OS_MAC)

process->start("open", QStringList() << path);

#endif

How I can achieve the same behavior for let say Ubuntu?

Gad D Lord
  • 6,620
  • 12
  • 60
  • 106

1 Answers1

40

Use QDesktopServices and its openUrl function:

QString path = QDir::toNativeSeparators(QApplication::applicationDirPath());
QDesktopServices::openUrl(QUrl::fromLocalFile(path));

It should work with all OS'es. I have tested it only in Windows.

Phlucious
  • 3,704
  • 28
  • 61
  • 2
    Works like charm on Windows XP SP2, Mac OSX 10.6.4, and Ubuntu. – Gad D Lord Aug 25 '10 at 21:53
  • Hmm... for me (on Windows), this is actually opening the file using the default file association. For instance, it's opening a .wav file in Media Player rather than navigating to it in Windows Explorer. Any idea how to get the Explorer-like behaviour? – aardvarkk Apr 02 '14 at 17:46
  • I guess that you have to pass the directory containing the file as argument instead of the file itself. – J. Martinot-Lagarde May 13 '14 at 09:06