2

I need to connect some Checkboxes, so when I click one it becomes checked and other become unchecked. My code right now looks like this.

Connect in class constructor:

connect(cb_thickness1,SIGNAL(stateChanged(int)),this,SLOT(cb_thickness1_isChecked()));
connect(cb_thickness2,SIGNAL(stateChanged(int)),this,SLOT(cb_thickness2_isChecked()));
connect(cb_thickness3,SIGNAL(stateChanged(int)),this,SLOT(cb_thickness3_isChecked()));

and slots

void MainWind::cb_thickness1_isChecked()
{
    if(cb_thickness2->isChecked())
        cb_thickness2->setChecked(false);
    if(cb_thickness3->isChecked())
        cb_thickness3->setChecked(false);
}
void MainWind::cb_thickness2_isChecked()
{
    if(cb_thickness1->isChecked())
        cb_thickness1->setChecked(false);
    if(cb_thickness3->isChecked())
        cb_thickness3->setChecked(false);

}
void MainWind::cb_thickness3_isChecked()
{
    if(cb_thickness1->isChecked())
        cb_thickness1->setChecked(false);
    if(cb_thickness2->isChecked())
        cb_thickness2->setChecked(false);

}

Code doesn't work as expected. When I click to any ChBx first time, everything is OK, but when I click to other next time it only uncheck previous and does nothing with itself. Only on second click it become chekced.

Also I found one more bug, when I check to ChBox, and then uncheck it by clicking it againg, I can check 2 ChBxes. [pic 2]

Form Two ChBx checked

2
  • 4
    why not using radio button which are design for kind of use ? Commented Feb 9, 2017 at 8:36
  • I dodn't try use radio button, but it's good idea, thank you. Commented Feb 9, 2017 at 8:59

3 Answers 3

1

Radio button is a great idea.

But if you really want to use check box, you can explicitly set cb_thickness1 checked in cb_thickness1_isChecked(), and do the same for other two check boxes.

void MainWind::cb_thickness1_isChecked()
{
    cb_thickness1->setChecked(true);
    cb_thickness2->setChecked(false);
    cb_thickness3->setChecked(false);
}
void MainWind::cb_thickness2_isChecked()
{
    cb_thickness1->setChecked(false);
    cb_thickness2->setChecked(true);
    cb_thickness3->setChecked(false);
}
void MainWind::cb_thickness3_isChecked()
{
    cb_thickness1->setChecked(false);
    cb_thickness2->setChecked(false);
    cb_thickness3->setChecked(true);
}
Sign up to request clarification or add additional context in comments.

Comments

0

I suggest to derive a class from QCheckBox (lets call it CustomCheckBox) and add a signal, private slot and public slot

signal:

void enabled();

private slot:

void CustomCheckBox::checkEnable(bool state)
{
    if(state)
    {
        emit enabled();
    }
}

public slot:

void CustomCheckBox::uncheck()
{
    setChecked(false);
}

In the constructor add:

connect(this,SIGNAL(toggled(bool)),this,SLOT(checkEnable(bool)));

This way you can use simple connects.

CustomCheckBox *box1 = new CustomCheckBox();
CustomCheckBox *box2 = new CustomCheckBox();

connect(box1,SIGNAL(enabled()),box2,SLOT(uncheck()));

Feel free to improve this answer. :)

Comments

0

Verify that the state of the button is checked in the slot and then deactivate the other checkboxes like you already did. You can use the parameter of the stateChanged method by passing it to the slots.

Here is code that works: Variant I:

connect(ui->checkBoxA, SIGNAL(stateChanged(int)), this, SLOT(checkBoxAChanged(int)));
connect(ui->checkBoxB, SIGNAL(stateChanged(int)), this, SLOT(checkBoxBChanged(int)));
connect(ui->checkBoxC, SIGNAL(stateChanged(int)), this, SLOT(checkBoxCChanged(int)));

void MainWindow::checkBoxAChanged(int state)
{
if (state == Qt::Checked) {
    ui->checkBoxB->setChecked(false);
    ui->checkBoxC->setChecked(false);
}
}

void MainWindow::checkBoxBChanged(int state)
{
if (state == Qt::Checked) {
    ui->checkBoxA->setChecked(false);
    ui->checkBoxC->setChecked(false);
}
}

void MainWindow::checkBoxCChanged(int state)
{
if (state == Qt::Checked) {
    ui->checkBoxB->setChecked(false);
    ui->checkBoxA->setChecked(false);
}
}

Variant II:

connect(ui->checkBoxA, SIGNAL(clicked(bool)), this, SLOT(checkBoxAClicked(bool)));
connect(ui->checkBoxB, SIGNAL(clicked(bool)), this, SLOT(checkBoxBClicked(bool)));
connect(ui->checkBoxC, SIGNAL(clicked(bool)), this, SLOT(checkBoxCClicked(bool)));

void MainWindow::checkBoxAClicked(bool val)
{
if (val == true) {
    ui->checkBoxB->setChecked(false);
    ui->checkBoxC->setChecked(false);
}
}

void MainWindow::checkBoxBClicked(bool val)
{
if (val == true) {
    ui->checkBoxA->setChecked(false);
    ui->checkBoxC->setChecked(false);
}
}

void MainWindow::checkBoxCClicked(bool val)
{
if (val == true) {
    ui->checkBoxB->setChecked(false);
    ui->checkBoxA->setChecked(false);
}
}

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.