简体   繁体   English

QGroupBox 找到选中的单选按钮

[英]QGroupBox find selected Radio Button

I have created a simple UI consisting of a QGroupBox with a bunch of QRadioButtons (32 to be exact) and I want to be able to find the selected one.我创建了一个简单的 UI,由一个QGroupBox和一堆QRadioButtons (准确地说是 32 个)组成,我希望能够找到所选的一个。

I've looked at forums and things, but the answers I've found don't work, and one referenced documentation on a nonexistant method of QGroupBox .我查看了论坛和其他东西,但是我找到的答案不起作用,并且参考了一个关于QGroupBox不存在方法的文档。

Given the below snippet, how would I find the selected QRadioButton , if any?鉴于以下代码段,我将如何找到选定的QRadioButton (如果有)?

QGroupBox* thingGroup = ui->thingGroupBox;

If you want to get it when you select one of them you could use the toogled signal, connect it to some slot and use the sender () function and convert it to QRadioButton.如果您想在选择其中之一时获取它,您可以使用 toogled 信号,将其连接到某个插槽并使用 sender () 函数并将其转换为 QRadioButton。

*.h *。H

public slots:
    void onToggled(bool checked);

*.cpp *.cpp

QGroupBox *thingGroup = ui->groupBox;

QVBoxLayout *lay = new QVBoxLayout;

thingGroup->setLayout(lay);

for(int i = 0; i < 32; i++){
    QRadioButton *radioButton = new QRadioButton(QString::number(i));
    lay->addWidget(radioButton);
    connect(radioButton, &QRadioButton::toggled, this, &{your Class}::onToggled);
}

slot:投币口:

void {your Class}::onToggled(bool checked)
{
    if(checked){
        //btn is Checked
        QRadioButton *btn = static_cast<QRadioButton *>(sender());
    }

}

I guess it is easier to identify which button is checked using a QButtonGroup , just remember to select buttons in "Design mode" then right-click and select assign to button group :我想更容易识别使用QButtonGroup检查哪个按钮,只需记住在“设计模式”中选择按钮,然后右键单击并选择assign to button group

在此处输入图像描述

To identify the checked button, you can use QButtonGroup's checkedButton method:要识别选中按钮,可以使用 QButtonGroup 的checkedButton方法:

QAbstractButton *button = ui->buttonGroup->checkedButton();

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

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