Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QT clear widget contents

Tags:

c++

qt

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?

like image 688
sark9012 Avatar asked Nov 01 '25 09:11

sark9012


2 Answers

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

like image 121
cppguy Avatar answered Nov 02 '25 23:11

cppguy


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

like image 44
Oleg Shparber Avatar answered Nov 03 '25 00:11

Oleg Shparber