2

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? 

1 Answer 1

1

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

)

Sign up to request clarification or add additional context in comments.

5 Comments

What does it mean "automatically"? While saving with QSettings you need to call setValue() for each variable (with unique pair section-key). And you don't need to do somthing else
Saving means the setting actually gets written somewhere outside program memory (eg. a file). So what I'm asking is whether this happens automatically and if so, I wonder when... Otherwise I need to know how to save the settings (eg. to a file, system registry or whatever is Android using).
No, you don't need to do somthing else than invoking setValue(). QSettings will automatically make writing to a registry (or to a file, etc.). UPD: my be on Android need settings.sync();
And that Update: settings.sync(); means what exactly?
Perhaps, may be for Android it is necessary to call a function settings.sync() after saving. It my be needed if you want to load some saved settings before the QSettings's destructor will be called

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.