[Solved] How to save checkbox/radiobutton states with QSettings?
-
If the file does not exist, you will have to provide a default anyways. So just adjust the default value of the value("name", default) method call. If you change it from false to true your checkbox is checked by default.
-
So you mean something like:
@ui->preview->setChecked(settings.value("preview", true).toBool());@
? I am not sure... -
Oh come on! Just try it out! That's quicker than writing a comment here and waiting for an answer!
-
So for the checkboxes that are checked by default i should have true and for the ones that are not checked false? Right?
EDIT: I checked before adding the comment
-
[quote author="Leon" date="1309953483"]So for the checkboxes that are checked by default i should have true and for the ones that are not checked false? Right?
EDIT: I checked before adding the comment[/quote]
Sounds logical? no?
-
It works for me so propably yes! :)
Out of topic.. What about a slider?
What if i want to have as default value 6?
@ui->timerSlider->setValue(settings.value("timeSlider").toInt());@EDIT: If the file doesn't exist the value of the slider will be set to 1
-
Then pass 6 as the default. Surprising?
-
I edited my answer..
If the file doesn’t exist the value of the slider will be set to 1 even if the default one is 6 -
What i did is this:
@QFile timeslide ( QDir::homePath()+"/.config/Program/MainWindow.conf" );
if (timeslide.exists()) ui->timerSlider->setValue(settings.value("timeSlider").toInt());@If there is another way tell me!
-
Let's reformat that code a bit to make it more readable:
@
QFile timeslide ( QDir::homePath()+"/.config/Program/MainWindow.conf" );
if (timeslide.exists()) {
ui->timerSlider->setValue(settings.value("timeSlider").toInt());
}
@That makes it obvious that line 3 is not executed if the file does not exist. Hence, the value of the slider will not change. If you want to set the slider to some value indepent of the existance of the file, then you need to make sure you execute that line in a code path that doesn't only execute if the file exists.
-
I am reformating it at my code not here at the forum!
No i just want for the first time the default value to be 6.. After that the file will exist so if the user change it eg to value 4 the next time it will be value 4..
Ok thanks again! -
This works perfectly for me:
@
QSettings testSettings;
int sliderVal = testSettings.value("slider/value", 6).toInt();
qDebug() << "sliderVal=" << sliderVal;
ui->horizontalSlider->setValue(sliderVal);
@Be sure NOT to call or trigger any slot connected to signal valueChanged() before you have set the default value.