How to delete widgets from Qvboxlayout
-
wrote on 1 Aug 2014, 10:15 last edited by
Hi friends,
i am facing the one issue,,how to delete all widget from qvboxlayout in Qt,,After deleteing the widget i need to add some widget in the same Qvboxlayout,,now i delete the widget from Qvboxlayout,,and after that i add the new widget means Qvboxlayout take more spaces between the two widgets,, In below i added the code for deleting the widget from layout
@QLayoutItem* item;
while ( ( item = rightui->listLayout->takeAt( 0 ) ) != NULL )
{
if (item->widget()) {
qDebug() << "item widget: " << item->widget();
delete item->widget();
}
delete item;
}@
Please Give me a good solution for this,,,Thanks -
Try this.
@
int count = vlayout.count();
for(int i=0;i<count;i++) {QLayoutItem *item = vlayout.itemAt(I) vLayout.removeItem(item) delete item;
}@
-
[quote author="Dheerendra" date="1406900857"]Try this.
@
int count = vlayout.count();
for(int i=0;i<count;i++) {QLayoutItem *item = vlayout.itemAt(I) vLayout.removeItem(item) delete item;
}@[/quote]
Ouch. No. Don't do that. When you delete item 0 item 1 becomes item 0. You advance to delete item 1 and leave item 0 where it was. When you arrive at (half+1) item the itemAt(i) refers beyond the children count and referencing it will crash.The proper way is to either iterate backwards, deleting the last item or, like the OP did, delete first item until there are no left. that's what "the doc suggests":http://qt-project.org/doc/qt-5/qlayout.html#takeAt
You don't need to delete item and the widget separately. When you delete an item it will delete the widget.
As for the problem - I don't quite understand. What do you mean the layout takes more space? Can you give a simple example?
-
oops chris. This super goof-up from my side. Thank you Chris for pointing out.
2/4