首先从QButtonGroup移除全局子button比较容易,直接remove就行。
但是如果我们是动态往QButtonGroup中移除指定的子button,则相对比较麻烦。
- 动态往QButtonGroup中增加子button
QButtonGroup * btnGpCommandOperate;
btnGpCommandOperate = new QButtonGroup ;
void TCPMachine::commandTablePadding(QStringList strCommand)
{
//局部新建的按钮,所以不能直接当做索引
QPushButton * btnDelete = new QPushButton();
QPushButton * btnModify = new QPushButton();
btnDelete->setText(QStringLiteral("删除"));
btnModify->setText(QStringLiteral("| 修改"));
btnGpCommandOperate->addButton(btnDelete, 2* commandOrder);
btnGpCommandOperate->addButton(btnModify, 2 * commandOrder + 1);
}
- 采用buttons函数获得指定子button的索引,同时从QButtonGroup中移除子button
void TCPMachine::btnGpOperateCall(int buttonID)
{
//从按钮组中删除对应按钮
QList<QAbstractButton *> buttonList = btnGpCommandOperate->buttons(); //查看按钮组中的子按钮,并获得指定按钮的索引
btnGpCommandOperate->removeButton(buttonList[buttonID]);
btnGpCommandOperate->removeButton(buttonList[buttonID + 1]);
QList<QAbstractButton *> buttonList2 = btnGpCommandOperate->buttons(); //查看按钮组中的子按钮
}
- 采用std::vector获得指定子button的索引,同时从QButtonGroup中移除子button
QButtonGroup * btnGpCommandOperate;
btnGpCommandOperate = new QButtonGroup ;
std::vector< QPushButton* > btnDeleteVector;
std::vector< QPushButton* > btnModifyVector;
void TCPMachine::commandTablePadding(QStringList strCommand)
{
//局部新建的按钮,所以不能直接当做索引
QPushButton * btnDelete = new QPushButton();
QPushButton * btnModify = new QPushButton();
btnDelete->setText(QStringLiteral("删除"));
btnModify->setText(QStringLiteral("| 修改"));
btnGpCommandOperate->addButton(btnDelete, 2* commandOrder);
btnGpCommandOperate->addButton(btnModify, 2 * commandOrder + 1);
//将子按钮也加入容器中
btnDeleteVector.push_back(btnDelete);
btnModifyVector.push_back(btnModify );
}
void TCPMachine::btnGpOperateCall(int buttonID)
{
//从按钮组中删除对应按钮
btnGpCommandOperate->removeButton(btnDeleteVector[buttonID]);
btnGpCommandOperate->removeButton(btnModifyVector[buttonID]);
QList<QAbstractButton *> buttonList2 = btnGpCommandOperate->buttons(); //查看按钮组中的子按钮
}