I recently switched to Qt5 and I noticed that initializing QApplication is taking noticeably longer (on the order of minutes) compared to Qt4.
Looking into it, the program seems to be spending its time here:
Qt5Guid.dll!QGlobalStatic<QFactoryLoader,&`anonymous namespace'::Q_QGS_directLoader::innerFunction,A0x95ca5a10::Q_QGS_directLoader::guard>::operator()() Line 129 C++
Qt5Guid.dll!QPlatformIntegrationFactory::create(const QString & platform, const QStringList & paramList, int & argc, char * * argv, const QString & platformPluginPath) Line 70 C++
Qt5Guid.dll!init_platform(const QString & pluginArgument, const QString & platformPluginPath, const QString & platformThemeName, int & argc, char * * argv) Line 1019 C++
Qt5Guid.dll!QGuiApplicationPrivate::createPlatformIntegration() Line 1176 C++
Qt5Guid.dll!QGuiApplicationPrivate::createEventDispatcher() Line 1196 C++
Qt5Widgetsd.dll!QApplicationPrivate::createEventDispatcher() Line 197 C++
Qt5Cored.dll!QCoreApplication::init() Line 769 C++
Qt5Cored.dll!QCoreApplication::QCoreApplication(QCoreApplicationPrivate & p) Line 689 C++
Qt5Guid.dll!QGuiApplication::QGuiApplication(QGuiApplicationPrivate & p) Line 570 C++
Qt5Widgetsd.dll!QApplication::QApplication(int & argc, char * * argv, int _internal) Line 569 C++
It's basically spending several minutes every startup initializing the windows platform plugin.
How can I reduce or remove this time and does anyone know what the cause might be?
I'm using Qt 5.5.1 with VS2012 64 bit.
Edit: Not sure if it's relevant, but I have QT_QPA_PLATFORM_PLUGIN_PATH="C:\Qt-5.5.1\plugins\platforms\" which is how it finds the platform plugin.
Edit2: Further investigation shows that the cause is that in the process of initializing the program, QFactoryLoader::update()
scans through every file in some directories looking for a pattern. Because the directory includes the program directory it scans through the entirety of my program file (several gb).
The paths are determined by QCoreApplication::libraryPaths()
which returns has 3 values:
- C:/Qt-5.5.1/plugins/platforms
- C:/Qt-5.5.1/plugins
- C:/MyProject/x64/Debug <-- the problem
The update() function is called 7 times in the init process, but it's actually only the first two calls that are very slow.
I've also noticed that if I call QCoreApplication::libraryPaths()
before the init, then only the first two paths are returned for the first 4 update()
calls, which solves the problem.
It's great that I have a solution now, but I would like to understand why this works and find a less hacky one.