你好,这是我的代码,我在按钮事件处理程序中调用所有这些语句,如下所示
void analysis::on_pushButton_clicked()
{
myplot * p = new myplot(gao.structpayloadgraph,gao1.structpayloadgraph, gao.structcol-2, "payload");
myplot * p1 = new myplot(gao.structsessiongraph,gao.structsessiongraph ,gao.structcol-2, "session");
QHBoxLayout * layout = new QHBoxLayout;
ui->horizontalLayout_2->addLayout(layout);
layout->addWidget(p);
layout->addWidget(p1);
}myplot是一个图形绘制类,但问题是,每次我点击按钮时,新的图形会出现,之前的图形会保留下来,就像一个!st click 2在第二次出现时就变成了4然后6......如何销毁按钮事件处理程序中的QHBoxLayout
谢谢
发布于 2011-06-27 20:11:07
尝试只创建一个布局,并在每次单击后尝试执行类似于此布局->removeWidget(...);的操作来删除prew图。
发布于 2011-06-27 20:17:14
为on_pushButton_clicked()函数设置全局layout。
然后从其中删除所有以前的小部件:
QLayoutItem *item;
QLayoutIterator it = layout->iterator();
while((item = it.takeCurrent()) != 0) {
layout->remove(item->widget());
delete item->widget();
}然后,您可以添加您的小部件:
layout->addWidget(p);
layout->addWidget(p1);更新:仅适用于Qt3Support模式的。
UPD2:
QLayoutItem *tItem;
while (tItem = layout->takeAt(0) != 0)
delete tItem;发布于 2011-12-05 17:48:16
你应该这样做:
在你的班上:
class analysis{
private:
...
QHBoxLayout* hLayouot;
...
public:
...
};
in the constructor you have to create the object:
hLayout = new QHBoxLayout(this); --> if you can t put 'this' on constructor because your class doesn t hinerit from QWidget, you MUST delete hLayout inside the destructor!
while in your method void analysis::on_pushButton_clicked() you can call hLayout->removeWidget() ..
i have experienced problem too removing from layouts: so i called hLayout->clear() and then re-inserted the objects!https://stackoverflow.com/questions/6492530
复制相似问题