sark9012
sark9012

Reputation: 5747

QT clear widget contents

I have QWidget renameWidget which is referred to as ui->renameWidget. Within this, I have a QVBoxLayout *renamebox and within this I have several labels and textedits.

What I need to happen is that when I hit a button to submit these textedits, I need everything within the QWidget to be deleted. This will give the effect of the box being emptied or cleared.

I've tried to just delete the vboxlayout and I've also tried this:

qDeleteAll(ui->renameWidget->findChildren<QVBoxLayout *>());

Nothing has worked, any ideas?

Upvotes: 4

Views: 3564

Answers (2)

Oleg Shparber
Oleg Shparber

Reputation: 2752

qDeleteAll(ui->renamebox->children()); would delete all children.

Upvotes: 1

cppguy
cppguy

Reputation: 3713

try

 qDeleteAll(ui->renamebox->findChildren<QLabel *>());
 qDeleteAll(ui->renamebox->findChildren<QTextEdit *>());

Although it's typically better to call deleteLater on most QObject based classes because it allows the objects to be cleaned up in the next pass through the event loop, not in the middle of an event being processed

Upvotes: 4

Related Questions