[Solved] How to save checkbox/radiobutton states with QSettings?
-
wrote on 20 Jun 2011, 17:05 last edited by
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! -
wrote on 20 Jun 2011, 17:13 last edited by
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.
-
wrote on 20 Jun 2011, 17:14 last edited by
QSettings uses QVariant and QVariant supports bool.
-
wrote on 20 Jun 2011, 17:31 last edited by
Ok thanks a lot! It works!
Should i change toString() to toBool() when importing the settings? -
wrote on 20 Jun 2011, 17:34 last edited by
If you expect a boolean, yes you should.
-
wrote on 20 Jun 2011, 17:48 last edited by
You mind me asking why on earth you are storing the buttons text propery to QSettings?
-
wrote on 20 Jun 2011, 17:55 last edited by
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
-
wrote on 20 Jun 2011, 18:01 last edited by
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...
-
wrote on 20 Jun 2011, 18:43 last edited by
I Just think this is the right thing to do..! And it looks perfect at my app! So i will keep it like this.
-
wrote on 6 Jul 2011, 11:47 last edited by
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?
-
wrote on 6 Jul 2011, 11:50 last edited by
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.
-
wrote on 6 Jul 2011, 11:54 last edited by
So you mean something like:
@ui->preview->setChecked(settings.value("preview", true).toBool());@
? I am not sure... -
wrote on 6 Jul 2011, 11:56 last edited by
Oh come on! Just try it out! That's quicker than writing a comment here and waiting for an answer!
-
wrote on 6 Jul 2011, 11:58 last edited by
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
-
wrote on 6 Jul 2011, 12:03 last edited by
[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?
-
wrote on 6 Jul 2011, 12:18 last edited by
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
-
wrote on 6 Jul 2011, 12:21 last edited by
Then pass 6 as the default. Surprising?
-
wrote on 6 Jul 2011, 12:22 last edited by
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 -
wrote on 6 Jul 2011, 12:54 last edited by
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!
-
wrote on 6 Jul 2011, 13:00 last edited by
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.