[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.
-
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...
-
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?
-
-
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.