[英]QGroupBox find selected Radio Button
我創建了一個簡單的 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());
}
}
我想更容易識別使用QButtonGroup檢查哪個按鈕,只需記住在“設計模式”中選擇按鈕,然后右鍵單擊並選擇assign to button group
:
要識別選中按鈕,可以使用 QButtonGroup 的checkedButton方法:
QAbstractButton *button = ui->buttonGroup->checkedButton();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.