[Solved] How to save checkbox/radiobutton states with QSettings?
-
Hi!
I know how to use QSettings.. For example to save a text from a button at the close event i have this:
@settings.setValue( "browsesoundButton", ui->browsesoundButton->text() );@And at the constructor i have this
@ ui->browsesoundButton->setText( settings.value( "browsesoundButton" ).toString() );@But is there any way i can save checkbox/radiobutton states with QSettings?
You know if the checkbox is checked and you close the app and reopen it, the checkbox must be checked...
Thanks! -
Store isChecked() in a setting. You can read it as a boolean:
@settings.setValue("myCheckBox", myCheckBox->isChecked());
myCheckBox->setChecked(settings.value("myCheckBox", false /* default value */).toBool());@
If you use the tristate version you should use checkedState.
-
QSettings uses QVariant and QVariant supports bool.
-
Ok thanks a lot! It works!
Should i change toString() to toBool() when importing the settings? -
If you expect a boolean, yes you should.
-
You mind me asking why on earth you are storing the buttons text propery to QSettings?
-
Andre i have a button that when you press it you select an audio file.. When you click ok the text of the button changes to the path of the audio file.. So when you close the app and reopen i need the button to have the path of the sound that was previously selected ! :)
P.S Thanks Franzk
-
That sounds like a weird interface to me... What does a button with a file name on it do? Play it? No, in your case, it allows to (presumably) select another one. Not very intuitive IMHO. Also, from a programming perspective, not the nicest separation on concerns. Why not store the file name name where it makes sense (some class variable or better yet, property) at runtime and store that to QSettings when you need to. From that, you can simply re-set the text for the button. If you ever decide you don't want that file name on the button after all, there is only one piece of code to change...
-
I Just think this is the right thing to do..! And it looks perfect at my app! So i will keep it like this.
-
What if the .conf file doesn't exist? ( First time running the program )
Even if the default check state of a checkbox is checked and the .conf doesn't exist
@ui->preview->setChecked(settings.value("preview", false).toBool());@
will make it unchecked...So should i check for file existence?
-
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.