标题:如何动态删除QGridLayout里的控件
作者:alexltr
日期:2010-12-08 22:17
内容:
我在QGridLayout里添加了很多QPushButton。
请问如何在运行时将这些PushButton全部删除。
我用removeItem试了很久都不行,请各位指点一下。谢谢。
void SizeLayout::removeAllButtons()
{
while(sizeLayout->count()>0)
{
sizeLayout->removeItem(0);
}
}
#1 [roywillow 12-08 22:31]
感觉应该是removeWidget(),并且需要传递移除的widget的指针,而不是0
还有一些细节的东西最好参考一下assistant
#2 [alexltr 12-11 15:41]
试了几天了都不行,大家帮帮我吧!!!
以下这个是跟assitant做的,也不行。
void SizeLayout::removeAllButtons()
{
QLayoutItem *sizeBtn;
while((sizeBtn=sizeLayout->takeAt(0)) !=0)
{
delete sizeBtn;
}
}
以下这种方法加以下测试语句(已注释掉)后可以看到被删除的效果,但最后一个控件无法删除。
但如果注释掉以下两句语句,就看不到任何反应了。
void SizeLayout::removeAllButtons()
{
while(sizeLayout->count())
{
//QString num;
//QMessageBox::information(this,"test",num.setNum(sizeLayout->count()));
sizeLayout->removeItem(sizeLayout->itemAt(0));
}
}
#3 [alexltr 12-11 16:33]
终于找到了!!! 用以下语句可以达到我要的效果。
但这样只是隐藏起来,不知是否有问题。
sizeLayout->itemAt(i)->widget()->hide()
#4 [cofei 01-07 17:50]
得到每个ITEM , 先把每个item的parent设为0,再delete掉
#5 [alexltr 01-09 14:22]
Thank you very much, cofei.
想不到过了这么久还有人帮我,谢谢!!!
其实我一直觉得我的程序有问题,因为程序越运行所占的内存越大。可能是LAYOUT里面的控件越来越多。虽然隐藏了,但无法删除。我再用用cofei的方法试一下。