0

I have some difficulty settings in a game. Because only one option should be active at once i did e.g. for option 'Easy'

//Uncheck all other difficulty options
if(ui->actionMedium->isChecked())
{
    ui->actionMedium->setChecked(false);
}

if(ui->actionHard->isChecked())
{
    ui->actionHard->setChecked(false);
}

Now the problem is that if i click on an already checked checkbox the checkbox will uncheck. Ok, thats the normal behaviour of a checkbox, so i added

//check if its alreay checked
if(ui->actionEasy->isChecked())
{
    ui->actionEasy->setChecked(true);
}

but this doent work and i dont understand why.

As alternativ i could group radiobuttons, but i would like to unterstand why my code doent work.

4
  • 1
    To select one of several mutually exclusive options you should use radio buttons, not checkboxes. Using checkboxes incorrectly by adding custom behavior is a bad idea. Commented Jul 8, 2013 at 21:14
  • What is last code snippet supposed to do? If easy is checked set easy checked? It doesn't make much sense. Commented Jul 8, 2013 at 21:24
  • you should add settings changes for all three in every function. So if one is set checked, the other are set unchecked in the same if. But I also agree with @bames53 Commented Jul 8, 2013 at 21:24
  • I thought that if i do this a checkbox is uncheckable if its already checked. Unfortunately i was wrong. It seems that Qt will check if the checkbox is checked after the code will be executed and so the code as you said is useless ;). Commented Jul 8, 2013 at 21:30

3 Answers 3

1

If you uncheck the checkbox actionEasy ui->actionEasy->isChecked() == false and so your code doesn't check this checkbox.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. So Qt switches the check state before the code is executed.
1

You should take a look here it will teach you how to group items and make just one checkable at the same time

Comments

0

Disable checkbox once it is checked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.