Qt中动态增加或移除QButtonGroup(按钮组)中指定子button(按钮)

首先从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();	//查看按钮组中的子按钮
}

 

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值