Tomáš Zato
Tomáš Zato

Reputation: 53119

How and where do I save QSettings on Android?

I am starting to make a settings form for my program but I'm kinda stuck with the setting data. I can make a QSettings instance but what am I supposed to do with it then?

QApplication app(argc, argv);
QSettings settings;
settings.setValue("test", QVariant((int)42));
// Now what?

Upvotes: 2

Views: 3639

Answers (1)

Vladimir Bershov
Vladimir Bershov

Reputation: 2832

Normally, you don't need to know where the settings are stored. Usual using of QSettings: you need to set your organization name well as the name of your application. While saving you need to set section and key, and a parameter.

//set names
QCoreApplication::setOrganizationName("MySoft");
QCoreApplication::setApplicationName("Star Runner");
//...
QSettings settings;
//saving
settings.setValue("MySection/MyKey", 42);
//loading: section, key, and default value (default value will be used if the setting doesn't exist)
int val = settings.value("MySection/MyKey", 0).toInt();

Update

//Perhaps, for Android it is necessary to call a function
settings.sync();

(From the documentation:

void QSettings::sync() 

Writes any unsaved changes to permanent storage, and reloads any settings that have been changed in the meantime by another application. This function is called automatically from QSettings's destructor and by the event loop at regular intervals, so you normally don't need to call it yourself

)

Upvotes: 1

Related Questions