展会信息港展会大全

QT中获取选中的radioButton的两种方法
来源:互联网   发布日期:2016-01-26 10:27:18   浏览:3652次  

导读:方法一:采用对象名称进行获� ?RadioButton* pbtn = qobject_cast(ui-BG-checkedButton()); QString name = pbtn-objectName(); if(!QString::compare(name, radioButton)) { QMess ...

方法一:采用对象名称进行获取

QRadioButton* pbtn = qobject_cast(ui->BG->checkedButton());

QString name = pbtn->objectName();

if(!QString::compare(name, "radioButton"))

{

QMessageBox::information(this, "Tips", "red chosed!", QMessageBox::Ok);

}

else if(!QString::compare(name, "radioButton_2"))

{

QMessageBox::information(this, "Tips", "blue chosed!", QMessageBox::Ok);

}

else

{

QMessageBox::information(this, "Tips", "black chosed!", QMessageBox::Ok);

}

该代码片段中,首先使用qobject_cast将checkedButton()函数返回的QAbstractionButton转换为其子类类型QRadioButton.然后,获取被选中按钮的对象名。这可以通过获取objectName这个属性获龋再稍作判断即可得知结果。注:BG是手动添加的QGroupButton类型,radioButton和radioButton_2,radioButton_3都是UI中添加的 radioButton控件。

方法二:通过button的ID来获取

位于构造函数中的代码(初始选中第一个按钮):

ui->BG->setId(ui->radioButton, 0);

ui->BG->setId(ui->radioButton_2, 1);

ui->BG->setId(ui->radioButton_3, 2);

ui->radioButton->setChecked(true);

响应信号的槽函数或其他函数中的代码:

int a = ui->BG->checkedId();

switch(a)

{

case 0:

QMessageBox::information(this, "Tips", "Red chosed!", QMessageBox::Ok);

break;

case 1:

QMessageBox::information(this, "Tips", "blue chosed!", QMessageBox::Ok);

break;

case 2:

QMessageBox::information(this, "Tips", "black chosed!", QMessageBox::Ok);

break;

default:

break;

}

赞助本站

人工智能实验室

相关热词: radioButton QT

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港