[Solved] How to save checkbox/radiobutton states with QSettings?
-
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.
-
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.