Search code examples
c++qtcheckboxischecked

how can I verify if multiple checkboxes are checked


std::string output; 

if ((checkbox1->isChecked() && checkbox2->isChecked()) && 
   (!checkbox3->isChecked() || !checkbox4->isChecked() || !checkbox5->isChecked() || !checkbox6->isChecked()))
{
  output = " Using Checkbox: 1, 2 ";
}

if ((checkbox1->isChecked() && checkbox2->isChecked() && checkbox3->isChecked()) && 
   (!checkbox4->isChecked() || !checkbox5->isChecked() || !checkbox6->isChecked()))
{
  output = " Using Checkbox: 1, 2, 3 ";
}

....

using QT creator how can I verify how many checkboxes have been checked and change the output string accordingly? with multiple if statements it's not working due to me getting confused with all those NOT AND OR. and it takes a long time to code all possibilities.


Solution

  • All your checkBoxes should be in groupBox

    Try this:

    QList<QCheckBox *> allButtons = ui->groupBox->findChildren<QCheckBox *>();
    qDebug() <<allButtons.size();
    for(int i = 0; i < allButtons.size(); ++i)
    {
        if(allButtons.at(i)->isChecked())
            qDebug() << "Use" << allButtons.at(i)->text()<< i;//or what you need
    }