Qt-QButtonGroup

本文详细介绍了如何在Qt中使用QButtonGroup组件管理一组QRadioButton或QAbstractButton按钮,包括添加按钮、设置按钮ID、获取选中按钮的状态等基本操作,并提供了具体的代码实例。

1.包含头文件

#include <QButtonGroup>
2.
  QButtonGroup *buttonGroup;
1   buttonGroup = new QButtonGroup();
2     buttonGroup->addButton (ui.noSpace_Btn, 0);
3     buttonGroup->addButton (ui.smallSpace_Btn, 1);
4     buttonGroup->addButton (ui.mediumSpace_Btn, 2);
5     buttonGroup->addButton (ui.bigSpace_Btn, 3);

3/可以手动指定一个id被选 中

1  QRadioButton *path = qobject_cast<QRadioButton*> (buttonGroup->button (mSelectRadioButton));
2     path->setChecked (true);
 connect (buttonGroup, SIGNAL (buttonClicked (int)), this, SLOT (buttonJudge (int)));
 quint16 a = buttonGroup->checkedId();

上面的基本功能应该可以实现了。

资料解析:

常用方法 

void QButtonGroup::addButton ( QAbstractButton * button )

Adds the given button to the end of the group's internal list of buttons. An id will be assigned to the button by this QButtonGroup. Automatically assigned ids are guaranteed to be negative, starting with -2. If you are also assigning your own ids, use positive values to avoid conflicts.

如果不手动指定id,会有默认的从-2开始,建议手动指定,可以初始化也可以分开

void QButtonGroup::setId ( QAbstractButton * button, int id )
Sets the id for the specified button. Note that id can not be -1.

可以让其它互斥,也可以设定

bool exclusive () const
void setExclusive ( bool )

也可以移除:

void QButtonGroup::removeButton ( QAbstractButton * button )
Removes the given button from the button group.

其它的参考帮助手册,常用的也就是这么多

信号也有很多,看自己的情况使用

void QButtonGroup::buttonClicked ( int id ) [signal]可以拿到id

void QButtonGroup::buttonClicked ( QAbstractButton * button ) [signal]可以拿手button

还有其它的

void buttonPressed ( QAbstractButton * button )
void buttonPressed ( int id )
void buttonReleased ( QAbstractButton * button )
void buttonReleased ( int id )

问题,我在里面放QPushButton时设定id会一直返回65535也就是-1,证明没有拿到checkid,后来自己按钮少就改用:

1  QPushButton *qtt =    qobject_cast<QPushButton*> (sender());
2     
3     if ("strartStopAlarm_Btn" == qtt->objectName())
4     {
5         alarmTemp->StartStopTimer();
6     }

为了工作,先实现了功能 ,但感觉这不是好方法 ,看客如果有好方法,请回复我一下。

 

转载于:https://www.cnblogs.com/ants-double/p/5898610.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值