
T qobject_cast ( QObject object )如果object是T类型或者它的子类,就可以把object返回成T类型对象。否则返回0。类T必须是QObject的子类,而且必须声明宏:Q_OBJECTExample: Cpp代码 QObject obj = new QTimer; // QTimer inherits QObject QTimer timer = qobject_cast<QTimer >(obj); // timer == (QObject )obj QAbstractButton button = qobject_cast<QAbstractButton >(obj); // button == 0 问题 方法1、可以通过对象名称去判断Cpp代码 QAbstractButton radioButton = qobject_cast<QAbstractButton > (uibuttonGroup_1->checkedButton()); //uibuttonGroup_1->checkedButton() 返回一个QRadioButton对象 //将它转换成QAbstractButton //,通过对象名称去判断 if(QString::compare(radioButton->objectName(), "topTubePositionRadio", Qt::CaseSensitive)) tubePosition = 0; else if(QString::compare(radioButton->objectName(), "bottomTubePositionRadio", Qt::CaseSensitive)) tubePosition = 1; else if (QString::compare(radioButton->objectName(), "lateralTubePositionRadio", Qt::CaseSensitive)) tubePosition = 2;
方法2:通过checkedId去判断首先需要在界面被激活初始化设置buttonGroup中的IdCpp代码 uibuttonGroup_1->setId(uitopTubePositionRadio,0);//topTubePositionRadio的Id设为0 uibuttonGroup_1->setId(uibottomTubePositionRadio,1); uibuttonGroup_1->setId(uilateralTubePositionRadio,2);
然后在你想获取哪个radioButton被选中时直接获取checkedId值,最后判断一下这个Id值就可以了。quint16 a = uibuttonGroup_1->checkedId(); 很纳闷为啥QtDesigner中没有界面直接赋给这个radioButton,Id值?????或许是没有必要吧,第一种方法也可以。
void Widget::keyPressEvent(QKeyEvent event)
{
switch (event->key())
{
case Qt::Key_Return:
focusNextPrevChild(true);
break;}
}
focusNextPrevChild(true);这是选下一个焦点的,返回当前的就在这附近,你帮助里找下,我就帮你找了。
在 C# 中获取 RadioButton 控件选中的文本,可以通过以下代码实现:
在以上示例中,我们首先检查每个 RadioButton 是否被选中。如果某个 RadioButton 被选中,我们就可以使用 Text 属性来获取它的文本,并将其存储到一个字符串变量中。
注意,以上示例中的 radioButton1 和 radioButton2 是两个示例 RadioButton 控件的名称,您需要将其替换为您应用程序中实际使用的控件名称。
以上就是关于判断QButtonGroup中哪个QRadioButton被选中全部的内容,包括:判断QButtonGroup中哪个QRadioButton被选中、Qt中如何获取当前焦点控件、C#中radiobutton选中的文本怎么获取等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)