Qt Utilities  6.4.1
Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models
desktoputils.cpp
Go to the documentation of this file.
1 #include "./desktoputils.h"
2 
3 #include <QDesktopServices>
4 #include <QUrl>
5 #ifdef Q_OS_WIN32
6 #include <QFileInfo>
7 #endif
8 
9 namespace QtUtilities {
10 
21 bool openLocalFileOrDir(const QString &path)
22 {
23  QUrl url(QStringLiteral("file://"));
24 
25 #ifdef Q_OS_WIN32
26  // replace backslashes with regular slashes
27  QString tmp(path);
28  tmp.replace(QChar('\\'), QChar('/'));
29 
30  // add a slash before the drive letter of an absolute path
31  if (QFileInfo(path).isAbsolute()) {
32  tmp = QStringLiteral("/") + tmp;
33  }
34  url.setPath(tmp, QUrl::DecodedMode);
35 
36 #else
37  url.setPath(path, QUrl::DecodedMode);
38 #endif
39  return QDesktopServices::openUrl(url);
40 }
41 } // namespace QtUtilities
QT_UTILITIES_EXPORT bool openLocalFileOrDir(const QString &path)
Shows the specified file or directory using the default file browser.