Dr Casper Black
Dr Casper Black

Reputation: 7478

Empty scrollArea QT C++

I am loading data from xml executed by timer. xml is parsed and populated in entity object. In a loop im taking the data from entity object and populate QCommandLinkButton's. and in the end a batch of buttons are set into the verticalLayout and then in scrollArea.

but every time data is loaded its appends to the old data. How can I empty the content of the srollArea before repopulating scrollArea.

MainWindow::methodExecudedByTimer(){

    foreach(int i, map.keys()){ 
        QCommandLinkButton* buttonEmail = new QCommandLinkButton(this);
        Email em = map[i];
        buttonEmail->setText(em.__toString());

        ui->verticalLayout->addWidget(buttonEmail);
    }
    ui->scrollArea->setLayout(ui->verticalLayout);
}

Upvotes: 0

Views: 555

Answers (2)

Dr Casper Black
Dr Casper Black

Reputation: 7478

I found the answer in the depths of Internet.

before foreach i added this:

qDebug() << "check if layout filled";
if(ui->verticalLayout->count() > 0){
   qDebug() << "empty layout";
   QLayoutItem *item = NULL;
   while ((item = ui->verticalLayout->takeAt(0)) != 0) {
      delete item->widget();
   }
}else{
   // on app first run
   qDebug() << "layout empty";
}

Upvotes: 0

Magical
Magical

Reputation: 263

you can use setWidget replace setLayout.and then new data coming,you can call takeWidget to remove old data. MainWindow::methodExecudedByTimer(){

foreach(int i, map.keys()){ 
    QCommandLinkButton* buttonEmail = new QCommandLinkButton(this);
    Email em = map[i];
    buttonEmail->setText(em.__toString());

    ui->verticalLayout->addWidget(buttonEmail);
}
ui->scrollArea->takeWidget();
QWidget *widget = new QWidget();
QSize size = ui->scrollArea->size();
widget->setMinimumSize(size.width(),size.height());
widget->setLayout(ui->verticalLayout);
ui->scrollArea->setWidget(widget);

}

Upvotes: 1

Related Questions