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     }

為了工作,先實現了功能 ,但感覺這不是好方法 ,看客如果有好方法,請回復我一下。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM