factory method to build and return a suitable Qt application instance it remembers a previously created instance, and will return it if available otherwise a new one is created and configure caution: cannot use .exec() any more, instead call .show() and use QApplication::exec()
| 99 | // otherwise a new one is created and configure |
| 100 | // caution: cannot use <widget>.exec() any more, instead call <widget>.show() and use QApplication::exec() |
| 101 | QCoreApplication* getApp(char** argv) { |
| 102 | if (QCoreApplication::instance() != nullptr) |
| 103 | return QCoreApplication::instance(); |
| 104 | |
| 105 | // build application version string |
| 106 | std::string version; |
| 107 | { |
| 108 | std::ostringstream oss; |
| 109 | oss << "version " << APPIMAGELAUNCHER_VERSION << " " |
| 110 | << "(git commit " << APPIMAGELAUNCHER_GIT_COMMIT << "), built on " |
| 111 | << APPIMAGELAUNCHER_BUILD_DATE; |
| 112 | version = oss.str(); |
| 113 | } |
| 114 | |
| 115 | QCoreApplication* app; |
| 116 | |
| 117 | // need to pass rvalue, hence defining a variable |
| 118 | int* fakeArgc = new int{1}; |
| 119 | |
| 120 | static char** fakeArgv = new char*{strdup(argv[0])}; |
| 121 | |
| 122 | if (isHeadless()) { |
| 123 | app = new QCoreApplication(*fakeArgc, fakeArgv); |
| 124 | } else { |
| 125 | auto uiApp = new QApplication(*fakeArgc, fakeArgv); |
| 126 | QApplication::setApplicationDisplayName("AppImageLauncher"); |
| 127 | |
| 128 | // this doesn't seem to have any effect... but it doesn't hurt either |
| 129 | uiApp->setWindowIcon(QIcon(":/AppImageLauncher.svg")); |
| 130 | |
| 131 | app = uiApp; |
| 132 | } |
| 133 | |
| 134 | QCoreApplication::setApplicationName("AppImageLauncher"); |
| 135 | QCoreApplication::setApplicationVersion(QString::fromStdString(version)); |
| 136 | |
| 137 | return app; |
| 138 | } |
| 139 | |
| 140 | int main(int argc, char** argv) { |
| 141 | // create a suitable application object (either graphical (QApplication) or headless (QCoreApplication)) |