PDA

View Full Version : Delete widgets from QVBoxLayout



mythili
20th May 2013, 08:23
Hi,
I am creating a QWidget in the constructor and adding that QWidget to QScrollArea. When the verticall scroll bar reaches to the maximum value, i am creating another QWidget and adding to the existing widget. All these are in vertical layout.

I want to delete all the widgets in the vertical layout created dynamically when i press a button.

My code :

writingArea is a another class.
writingArea::writingArea(QWidget *parent) :
QWidget(parent){ //constructor of writingArea class//}

//Declaration
MainWindow.h

writingArea *writewidget;
QVBoxLayout *vwritelayout;

//Constructor of MainWindow.h

writewidget = new writingArea;
writewidget->setMinimumSize(1000,1300);
vwritelayout->addWidget(writewidget);
connect(ui->answerarea_scrollArea->verticalScrollBar(),SIGNAL(actionTriggered(int)),t his,SLOT(add_write_area()));
connect(ui->pushbutton,SIGNAL(clicked()),this,SLOT(delete_widg ets()));

//Functions
void MainWindow::add_write_area()
{


int write_sliderval = ui->answerarea_scrollArea->verticalScrollBar()->maximum();
int write_sliderpos=ui->answerarea_scrollArea->verticalScrollBar()->sliderPosition();

if(write_sliderval == write_sliderpos)
{
writewidget = new writingArea;
writewidget->setMinimumSize(1000,1300);
vwritelayout->addWidget(writewidget);
}
}
void Mainwindow::delete_widgets()
{

//Here I want to delete the widgets created in veritelayout.

}

Please help me....

wysota
20th May 2013, 08:48
while(!vertilayout->isEmpty()) {
delete vertilayout->takeAt(0);
}

mythili
20th May 2013, 09:01
Its not coming inside the while loop

wysota
20th May 2013, 09:42
Apparently you have nothing in the layout.

ChrisW67
20th May 2013, 09:44
Then the layout contains no items and you don't need to do anything.