简体   繁体   English

如何找出用户选择的单选按钮

[英]How to find out which radio button chosen by the user

I have four radio buttons, the user must choose one of the four radio buttons. 我有四个单选按钮,用户必须从四个单选按钮中选择一个。

The problem is each radio button has its own name differs from the other. 问题是每个单选按钮都有自己的名称与另一个不同。

How to find out which radio button chosen by the user ? 如何找出用户选择的单选按钮?

Add the buttons into a GroupBox and use findChildren , after this you can use QButtonGroup or simply iterate through all Buttons list and check name of radiobutton . 将按钮添加到GroupBox并使用findChildren ,之后您可以使用QButtonGroup或只是遍历所有按钮列表并检查radiobutton名称。 It is efficient way because it works with 4 button or 1000, you should write big code if you have many buttons. 它是有效的方式,因为它适用于4按钮或1000,如果你有很多按钮你应该写大代码。

void MainWindow::on_pushButton_15_clicked(){
    QButtonGroup group;
    QList<QRadioButton *> allButtons = ui->groupBox->findChildren<QRadioButton *>();
    qDebug() <<allButtons.size();
    for(int i = 0; i < allButtons.size(); ++i)
    {
        group.addButton(allButtons[i],i);
    }
    qDebug() << group.checkedId();
    qDebug() << group.checkedButton();
}

You can use the ' isChecked() ' command that all qt buttons support, and check each radio button. 您可以使用所有qt按钮支持的' isChecked() '命令,并检查每个单选按钮。 Or, you can connect a function to the 'toggled(bool isChecked)' signal, and use that to update a value indicating which of the four radio buttons is checked. 或者,您可以将函数连接到'toggled(bool isChecked)'信号,并使用它来更新指示选中四个单选按钮中的哪一个的值。

The numeric values of the four IDs should be continuous. 四个ID的数值应该是连续的。 Given that, call GetCheckedRadioButton to determine which one is selected. 鉴于此,调用GetCheckedRadioButton来确定选择了哪一个。

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

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