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

1

5

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.

Garin answered 28/9, 2015 at 12:18 Comment(2)
Try to call QSettings::sync() between writing and reading the value.Ballman
Unfortunately, it doesn't work.Garin
N
12

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.

Nagpur answered 28/9, 2015 at 12:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.