Disregard Checkbox changes when Cancel is clicked.
-
Can someone help me with this? I want to disregard the changes in the checkbox when cancel button is clicked. I used the following codes but it didn't work.
void MainWindow::on_Cancel_clicked() { if (ui->checkBox->isChecked()){ ui->checkBox->setChecked(false); } else{ ui->checkBox->setChecked(true); } }
[Moved to General and Desktop ~kshegunov]
-
Hi,
What exactly doesn't work ?
-
Do you mean when you start your application again ?
-
That code is flawed. What if the user did not change the checkbox before hitting cancel? You are forcing a toggle basically when it's potential it wasn't clicked.
You should track the original state in a variable until either ok or cancel is pressed. If cancel, return it to it's original state, if ok, honor the change.
And finally, it does sound that you are trying to have a checkbox value persist after the app closes and reopens. If this is the case you need to save it's value somewhere. Check into
QSettings
for an easy way to handle serialization of options. -
@ambershark still has a point. Changing something that hasn't been modified is just plain wrong.
The use QSetting to store and load the state of the widget.