0.程序 main()
函数执行流程图
1.Restarter restarter(argc, argv)
1 2 3 4 5 6 int main (int argc, char **argv) { Restarter restarter (argc, argv) ; ...... return restarter.restartOrExit (app.exec ()); }
程序重启器,用于某些配置修改后,需要重启程序才能够使配置生效。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 class Restarter { public : Restarter (int argc, char *argv[]) { Q_UNUSED (argc) m_executable = QString::fromLocal8Bit (argv[0 ]); m_workingPath = QDir::currentPath (); } void setArguments (const QStringList &args) { m_args = args; } QString executable () const { return m_executable; } QStringList arguments () const { return m_args; } QString workingPath () const { return m_workingPath; } int restartOrExit (int exitCode) { return qApp->property ("restart" ).toBool () ? restart (exitCode) : exitCode; } int restart (int exitCode) { QProcess::startDetached (m_executable, m_args, m_workingPath); return exitCode; } private : QString m_executable; QStringList m_args; QString m_workingPath; };
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 SharedTools::QtSingleApplication app ((QLatin1String(Core::Constants::IDE_DISPLAY_NAME)), numberofArguments, options.appArguments.data()) ;...... #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) app.setAttribute (Qt::AA_UseHighDpiPixmaps); app.setAttribute (Qt::AA_DisableWindowContextHelpButton); #endif ...... QTranslator translator; QTranslator qtTranslator; ...... app.installTranslator (&translator); app.installTranslator (&qtTranslator); app.setProperty ("qtc_locale" , locale); ...... app.setDesktopFileName ("org.qt-project.qtcreator.desktop" ); bool isBlock = foundAppOptions.contains (QLatin1String (BLOCK_OPTION)); if (app.isRunning () && (pid != -1 || isBlock || foundAppOptions.contains (QLatin1String (CLIENT_OPTION)))) { app.setBlock (isBlock); if (app.sendMessage (PluginManager::serializedArguments (), 5000 , pid)) return 0 ; if (app.isRunning (pid)) { int button = askMsgSendFailed (); while (button == QMessageBox::Retry) { if (app.sendMessage (PluginManager::serializedArguments (), 5000 , pid)) return 0 ; if (!app.isRunning (pid)) button = QMessageBox::Yes; else button = askMsgSendFailed (); } if (button == QMessageBox::No) return -1 ; } } QObject::connect (&app, &SharedTools::QtSingleApplication::messageReceived, &pluginManager, &PluginManager::remoteArguments); QObject::connect (&app, SIGNAL (fileOpenRequest (QString)), coreplugin->plugin (), SLOT (fileOpenRequest (QString))); QObject::connect (&app, &QCoreApplication::aboutToQuit, &pluginManager, &PluginManager::shutdown); return restarter.restartOrExit (app.exec ());
QtSingleApplication 提供了每个用户只能启动一次的应用程序的功能。
Qt 提供了解决方案组件项目 qtproject/qt-solutions https://github.com/qtproject/qt-solutions
Qt Creator 里的 QtSingleApplication 和 qt-solutions 有些不一样
qtproject/qt-solutions 提供的解决方案项目在另外的篇章展开分析:QtSingleApplication(TODO)
3.程序工作环境配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 QCoreApplication::setApplicationName (Core::Constants::IDE_CASED_ID); QCoreApplication::setApplicationVersion (QLatin1String (Core::Constants::IDE_VERSION_LONG)); QCoreApplication::setOrganizationName (QLatin1String (Core::Constants::IDE_SETTINGSVARIANT_STR)); QGuiApplication::setApplicationDisplayName (Core::Constants::IDE_DISPLAY_NAME); auto cleanup = qScopeGuard ([] { Utils::Singleton::deleteAll (); });const QStringList pluginArguments = app.arguments ();setupInstallSettings (options.installSettingsPath);Utils::QtcSettings *settings = createUserSettings (); Utils::QtcSettings *globalSettings = new Utils::QtcSettings (QSettings::IniFormat, QSettings::SystemScope, QLatin1String (Core::Constants::IDE_SETTINGSVARIANT_STR), QLatin1String (Core::Constants::IDE_CASED_ID)); Utils::TerminalCommand::setSettings (settings); loadFonts ();
主要理解这两个代表什么就行了
QSettings::UserScope
QSettings::SystemScope
TODO:QSettings 需要另外的篇章介绍。
4.PluginManager pluginManager
1 2 3 4 5 6 7 8 9 10 11 PluginManager pluginManager; PluginManager::setPluginIID (QLatin1String ("org.qt-project.Qt.QtCreatorPlugin" )); PluginManager::setGlobalSettings (globalSettings); PluginManager::setSettings (settings); ... PluginManager::setPluginPaths (pluginPaths); ... PluginManager::checkForProblematicPlugins (); PluginManager::loadPlugins ();