Search code examples
c++qtqsettings

QSettings(Qt 5.4): setValue doesn't work properly


In my .cpp I'm using QSettings.
This worked before, in Qt 4.8:

#include <QSettings>


----------


QSettings settings;
settings.setValue("time_axis_direction", 1);
int test_var = settings.value("time_axis_direction").toInt();


----------

In test_var the program returns 0, what's the cause?
I used Qt with VS Add-In.


Solution

  • According to the docs, you have to set organization name and application name:

    QCoreApplication::setOrganizationName("My Organization");
    QCoreApplication::setApplicationName("My Application");
    QSettings settings;
    

    Or right in the constructor:

    QSettings settings("My Organization", "My Application");
    

    This will create HKCU\SOFTWARE\My Organization\My Application registry entry to store your settings (on Windows).

    If QCoreApplication::setOrganizationName() and QCoreApplication::setApplicationName() has not been previously called, the QSettings object will not be able to read or write any settings, and status() will return AccessError.