销毁QHBoxLayout

3

大家好,这是我的代码。我在我的按钮事件处理程序中调用了所有这些语句:

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是一个图形绘制类,但问题在于每次我点击按钮时都会出现新的图形,而之前的图形仍然存在,比如第一次点击时出现2个图形,第二次点击时它们变成了4个,然后是6个......

请问如何在我的按钮事件处理程序中销毁QHBoxLayout?

谢谢。

3个回答

0

将您的layout全局化,以供on_pushButton_clicked()函数使用。

然后从中删除所有先前的小部件:

    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模式。

更新2:

    QLayoutItem *tItem;
    while (tItem = layout->takeAt(0) != 0)
        delete tItem;

先生,它说QLayoutIterator不是类型名称,尽管我已经包含了#include"QLayoutIterator"类。 - sajid
哦,抱歉。我将它从旧的qt3代码中复制粘贴过来了。你可以使用Qt3Support模块或使用layout->remove(p)代替迭代。 - red1ynx
好的先生,但问题仍然存在,我应该在哪里写remove(p)?因为每次单击时都会创建新的布局,我需要删除该布局,我猜我该怎么办。 - sajid
我建议避免使用Qt3的东西,因为在下一个版本中它将被删除,这意味着它将停止编译。 - Vinicius Kamakura
是的,推荐使用“qt3支持层”作为Qt4应用程序的解决方案是...不好的... - Kamil Klimek

0

你应该这样做:

in your class :


class analysis{

私有部分: ... QHBoxLayout* hLayouot; ...

公共部分: ... };

在构造函数中,您必须创建对象:

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!


0
尝试创建一个布局,每次点击后尝试执行类似于layout->removeWidget(...)的操作来删除先前的图形。

这就是我在问的,先生,就像我不了解 Qt 函数一样。 - sajid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接