PDA

View Full Version : destroy QHBoxLayout



maarvi
27th June 2011, 15:26
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.structpayloadgr aph, gao.structcol-2, "payload");

myplot * p1 = new myplot(gao.structsessiongraph,gao.structsessiongra ph ,gao.structcol-2, "session");


QHBoxLayout * layout = new QHBoxLayout;
ui->horizontalLayout_2->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

Santosh Reddy
27th June 2011, 17:21
Not sure why you are adding layout to another layout. Add all the plots to a container widget, and delete the container widget as an when required, like this


analysis {
...
private:
QWidget * container;
}

void analysis::analysis()
{
container= 0;
}

void analysis::on_pushButton_clicked()
{
if(container)
delete container;

myplot * p = new myplot(gao.structpayloadgraph,gao1.structpayloadgr aph, gao.structcol-2, "payload");
myplot * p1 = new myplot(gao.structsessiongraph,gao.structsessiongra ph ,gao.structcol-2, "session");

QHBoxLayout * layout = new QHBoxLayout;
layout->addWidget(p);
layout->addWidget(p1);
container->setLayout(layout);

ui->horizontalLayout_2->addWidget(container);
}

maarvi
28th June 2011, 07:13
thanks u sir i wrote


while(!ui->horizontalLayout_3->isEmpty())
{ QWidget * w = ui->horizontalLayout_3->takeAt(0)->widget();
delete w;
}

and it worked for me thanks for your help