首页 > 代码库 > Qt-QButtonGroup

Qt-QButtonGroup

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     }

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

 

Qt-QButtonGroup