简体   繁体   English

如何验证是否选中了多个复选框

[英]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? 使用QT创建器如何验证已检查了多少个复选框并相应地更改输出字符串? with multiple if statements it's not working due to me getting confused with all those NOT AND OR. 由于我与所有那些NOT AND OR混淆,因此多个if语句无法正常工作。 and it takes a long time to code all possibilities. 编码所有可能性需要很长时间。

All your checkBoxes should be in groupBox 你的所有checkBoxes都应该在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
}

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 ) + ", ";
        }
    }

TLDR: Place them in a container and build your string by iterating over them. TLDR:将它们放在一个容器中,并通过遍历它们来构建字符串。

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();

Use QString instead of std::string and then: 使用QString而不是std::string然后:

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. 您可以将复选框保留在QList ,这是一个类字段,因此您不必每次都“构建” checkboxes数组。 You could also use QString::arg() instead of + operator for string when you build the output, etc, etc. 在构建输出等时,也可以使用QString::arg()而不是+运算符作为字符串。

What I've proposed is just a quick example. 我提出的只是一个简单的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM