gpt4 book ai didi

c++ - QGroupBox 找到选中的单选按钮

转载 作者:太空狗 更新时间:2023-10-29 21:35:18 25 4
gpt4 key购买 nike

我创建了一个简单的 UI,其中包含一个 QGroupBox 和一堆 QRadioButtons(确切地说是 32 个),我希望能够找到选定的一个。

我查看了论坛和其他东西,但我找到的答案不起作用,并且有一份引用文档介绍了 QGroupBox 的一个不存在的方法。

给定以下代码片段,我如何找到选定的 QRadioButton(如果有)?

QGroupBox* thingGroup = ui->thingGroupBox;

最佳答案

如果你想在选择其中一个时得到它,你可以使用 toogled 信号,将它连接到某个插槽并使用 sender () 函数并将其转换为 QRadioButton。

*.h

public slots:
void onToggled(bool checked);

*.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);
}

插槽:

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

}

关于c++ - QGroupBox 找到选中的单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43106746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com