关于c ++:如何找到Qt应用程序的安装目录?

How to find the installation directory of a Qt application?

我有一个基于Qt的应用程序,适用于Mac和Windows。 当用户安装软件时,它还会安装一个包含一堆HTML文档页面的文件夹。 我如何找到程序的安装位置,以便当用户尝试从应用程序中打开"帮助"时,将其带到index.html

我的程序安装在Windows和Mac的常规位置。 在Mac上,我的程序安装到/Users/username/Applications/MyProgram,其中MyProgram是包含" MyProgram.app"和" Doc"文件夹的文件夹。

1
2
3
4
5
6
7
#ifdef Q_OS_MACX
    docPath = executablePath +"/Doc/index.html";
#elif Q_OS_WIN
    docPath = executablePath +"/Doc/index.html";
#endif

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

因此,我的最终问题是executablePath应该是什么? 此外,这假设用户可以在默认位置之外的其他位置安装程序,或者可以从快捷方式启动程序。


您应该使用:

1
QString QCoreApplication::applicationDirPath() [static]