4
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.

4 Answers4

14

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
}
Jablonski
  • 18,083
  • 2
  • 46
  • 47
  • 1
    thanks, many good answers but yours was pretty short and clear *thumbs up* –  Sep 04 '14 at 11:22
  • Although it won't make any difference as far as performance I use `foreach(QCheckBox* pCheck,allButtons)` for that instead of the for loop. – drescherjm Sep 04 '14 at 12:00
  • 1
    @12dollar Before you develop a habit of using `QObject::findChildren`, please read [this answer](http://stackoverflow.com/a/19215870/2257050) first. I also find `QObject::findChildren` an ugly way of doing things which signals of poor design, but that may just be me. – thuga Sep 04 '14 at 13:26
2

Use an array of checkboxes like this

// h-file
#include <vector>
class MyForm {
...
    std::vector< QCheckBox* > m_checkBoxes;
};
// cpp-file
MyForm::MyForm() {
...
    m_checkBoxes.push_back( checkbox1 );
    m_checkBoxes.push_back( checkbox2 );
    ... 
    m_checkBoxes.push_back( checkbox5 );
}
...
    output = " Using Checkbox:";
    for ( int i = 0, size = m_checkBoxes.size(); i < size; ++i ) {
        if ( m_checkBoxes[ i ]->isChecked() ) {
            output += std::to_string( i + 1 ) + ", ";
        }
    }
borisbn
  • 4,988
  • 25
  • 42
1

TLDR: Place them in a container and build your string by iterating over them.

Code:

// line taken from @Chernobyl
QList<QCheckBox *> allButtons = ui->groupBox->findChildren<QCheckBox *>();

auto index = 1;
std::ostringstream outputBuffer;
outputBuffer << "Using Checkbox: ";
for(const auto checkBox: allButtons)
{
    if(checkBox->isChecked())
        outputBuffer << index << ", ";
    ++index;
}
auto output = outputBuffer.str();
utnapistim
  • 26,809
  • 3
  • 46
  • 82
0

Use QString instead of std::string and then:

QCheckBox* checkboxes[6];
checkbox[0] = checkbox1;
checkbox[1] = checkbox2;
checkbox[2] = checkbox3;
checkbox[3] = checkbox4;
checkbox[4] = checkbox5;
checkbox[5] = checkbox6;

QStringList usedCheckboxes;
for (int i = 0; i < 6; i++)
{
    if (checkbox[i]->isChecked())
        usedCheckboxes << QString::number(i+1);
}

QString output = " Using Checkbox: " + usedCheckboxes.join(", ") + " ";

This is just an example, but there's numerous ways to implement this. You could keep your checkboxes in the QList which is a class field, so you don't have to "build" the checkboxes array every time. You could also use QString::arg() instead of + operator for string when you build the output, etc, etc.

What I've proposed is just a quick example.

Googie
  • 5,742
  • 2
  • 19
  • 31