Retentive variable value
-
Hi,
I would like to retentive my
bool
variable. Restore it value after restart app.
Please tell my if below example is properly to retaininput1
value :MyApp::MyApp() { readSettings(); } void MyApp::writeSettings() { QSettings setting("RetVal", "values" ); setting.setValue("input1", input1); } void MyApp::readSettings() { QSettings setting("RetVal", "values" ); input1 = setting.value("input1").toBool(); } void MyApp:input1Changed(bool state) { input1 = state; writeSettings(); }
-
Apart from "RetVal" and "values" not being obviously good choices for organisation and application name respectively, I do not see anything wrong here.
I would generally set the organisation and application names on the application object (i.e. one place only) and use the parameter-free QSettings constructor elsewhere. See Basic Usage in the docs.
There is rarely a need to call QSettings::sync() directly. It will be called internally when the stack-based QSettings instances are destroyed anyway.
-
@Damian7546 just call "settings.sync()" after setting the value.
-
Apart from "RetVal" and "values" not being obviously good choices for organisation and application name respectively, I do not see anything wrong here.
I would generally set the organisation and application names on the application object (i.e. one place only) and use the parameter-free QSettings constructor elsewhere. See Basic Usage in the docs.
There is rarely a need to call QSettings::sync() directly. It will be called internally when the stack-based QSettings instances are destroyed anyway.
-