QApplication
derives from QCoreApplication
.
As you can see from the sources, applicationDirPath
is defined as:
QString QCoreApplication::applicationDirPath()
{
if (!self) {
qWarning("QCoreApplication::applicationDirPath: Please instantiate the QApplication object first");
return QString();
}
// ... more code
}
By going deeper into the code, we find that self
is initialized by the init
function, that is invoked by the constructor.
Because of that, it looks to me that it won't work as you expect it to do unless you have explicitly created an instance of a Q*Application
class (in this case, an instance of QApplication
).
Note from the documentation above that it is suggested to create such a class as soon as possible:
In general, we recommend that you create a QCoreApplication, QGuiApplication or a QApplication object in your main() function as early as possible. exec() will not return until the event loop exits; e.g., when quit() is called.
It means even before you try to access to QApplication::applicationDirPath()
, of course.
Otherwise you won't be able to get the right path from that method while using QSettings
.
QSettings
each time I need to use it instead of creating just one instance per class as a private member? – Dizzy