开发者

destroy QHBoxLayout

hello ever one here is my code i am calling all these statements in my button event handler like this

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-&g开发者_如何转开发t;addLayout(layout);
 layout->addWidget(p);
 layout->addWidget(p1);

}

myplot is graph plotting class but the problem is that each time i click the button new graph appear and previous remains, like one !st click 2 appear on second they become 4 then 6...... how to i destroy QHBoxLayout in my button event handler

thanks


Try to create only one layout and after each click try to perform something like this layout->removeWidget(...); for removing prew graph.


Make your layout global for on_pushButton_clicked() function.

Then remove all previous widgets from it:

    QLayoutItem *item;
    QLayoutIterator it = layout->iterator();

    while((item = it.takeCurrent()) != 0) {
        layout->remove(item->widget());
        delete item->widget();
    }

After than you can add your widgets:

    layout->addWidget(p);
    layout->addWidget(p1);

UPD: For Qt3Support mode only.

UPD2:

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


you should do something like :

in your class :


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜