QT5: Catch 22 regarding applicationDirPath and setLibraryPaths
I've been working on upgrading our app to QT5, and ran into an issue regarding the new platform libraries that are loaded on boot.
As far as I can tell, the platform plugin is scanned for and loaded as soon as the QGuiApplication object is first created. As we ship QT and all its plugins alongside our executable, we need to be able to show QT where the plugin will be by first calling QCoreApplication::setLibraryPaths.
Here's where it gets tricky. QCoreApplication doesn't allow you to call applicationDirPath before you instantiate your first QCoreApplication instance, meaning we can't find the location to pass to setLibraryPaths. Sure, we could use a relative path for the call, but our app has no guarantees of working directory.
At the moment, I am replicating the logic of applicationDirPath in my own code, but this can't be the best solution. Is there any way to access the application path prior to loading the platform DLL?
After fighting with this for hours (QCoreApplication and QGuiApplication don't seem to have ANY public members I could override and hook into), I came up with a really hacky solution that seems to work perfectly well.
As QCoreApplication doesn't rely on the platform library, and cleans up its singleton after it's deleted, this code seems to work:
QCoreApplication temp(argc, argv);
appDir = QCoreApplication::applicationDirPath();
// ~~ Set up library paths here using appDir as the root
I'm sure there's a little bit of overhead from creating two seperate application instances, but it saves a HUGE headache. Finding the application directory without applicationDirPath is not fun.
Have you considered to "file a bug report":http://bugreports.qt-project.org/ about this so that it is not missed?
You should be able to use QT_QPA_PLATFORM_PLUGIN_PATH and QT_QPA_PLATFORM environment variables as well if this is of use for you.
You won't be able to get the executable path without instantiating an object, as argv is required to determine the path on some platforms. But you can ship a qt.conf along with your application which explicitly sets a path for QLibraryInfo::PluginsPath.