Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

qt

qsettings

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.

like image 655
Saratery Avatar asked Sep 28 '15 12:09

Saratery


People also ask

How does QSettings work?

QSettings stores settings. Each setting consists of a QString that specifies the setting's name (the key) and a QVariant that stores the data associated with the key. To write a setting, use setValue().

Where are QSettings stored on Mac?

The QSettings class provides persistent platform-independent application settings. Users normally expect an application to remember its settings (window sizes and positions, options, etc.) across sessions. This information is often stored in the system registry on Windows, and in XML preferences files on Mac OS X.


1 Answers

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.

like image 79
kefir500 Avatar answered Sep 29 '22 06:09

kefir500