Correct way to remove content from layout?
-
I'm using Qt 5.9.2 on Windows 10 with MSVC2015. I have a function:
void MyClass::removeContent(QLayout* pLayout) { if ( pLayout == nullptr ) { return; } while( QLayoutItem* pItem = pLayout->takeAt(0) ) { if ( QWidget* pWidget = pItem->widget() ) { pWidget->deleteLater(); } else if ( QLayout* pChildLayout = pItem->layout() ) { removeContent(pChildLayout); pChildLayout->deleteLater(); } } }
I am seeing:
QFormLayout::takeAt: Invalid index 0
Is there anything I can do to prevent this message from occurring?
I've modified the initial if condition to:
if ( pLayout == nullptr || pLayout->children().size() == 0 ) {
[Edit], changed again to:
if ( pLayout == nullptr || pLayout->count() == 0 ) {
Still seeing the same thing.
-
@raven-worx thank you, just tried that, same result.
@SPlatten what about
while( pLayout->count() ) { QLayoutItem* pItem = pLayout->takeAt(0); if ( QWidget* pWidget = pItem->widget() ) { pWidget->deleteLater(); } else if ( QLayout* pChildLayout = pItem->layout() ) { removeContent(pChildLayout); pChildLayout->deleteLater(); } }
-
I'm using Qt 5.9.2 on Windows 10 with MSVC2015. I have a function:
void MyClass::removeContent(QLayout* pLayout) { if ( pLayout == nullptr ) { return; } while( QLayoutItem* pItem = pLayout->takeAt(0) ) { if ( QWidget* pWidget = pItem->widget() ) { pWidget->deleteLater(); } else if ( QLayout* pChildLayout = pItem->layout() ) { removeContent(pChildLayout); pChildLayout->deleteLater(); } } }
I am seeing:
QFormLayout::takeAt: Invalid index 0
Is there anything I can do to prevent this message from occurring?
I've modified the initial if condition to:
if ( pLayout == nullptr || pLayout->children().size() == 0 ) {
[Edit], changed again to:
if ( pLayout == nullptr || pLayout->count() == 0 ) {
Still seeing the same thing.
@SPlatten
pLayout->children().size()
should rather bepLayout->count()
-
@SPlatten
pLayout->children().size()
should rather bepLayout->count()
@raven-worx thank you, just tried that, same result.
-
I'm using Qt 5.9.2 on Windows 10 with MSVC2015. I have a function:
void MyClass::removeContent(QLayout* pLayout) { if ( pLayout == nullptr ) { return; } while( QLayoutItem* pItem = pLayout->takeAt(0) ) { if ( QWidget* pWidget = pItem->widget() ) { pWidget->deleteLater(); } else if ( QLayout* pChildLayout = pItem->layout() ) { removeContent(pChildLayout); pChildLayout->deleteLater(); } } }
I am seeing:
QFormLayout::takeAt: Invalid index 0
Is there anything I can do to prevent this message from occurring?
I've modified the initial if condition to:
if ( pLayout == nullptr || pLayout->children().size() == 0 ) {
[Edit], changed again to:
if ( pLayout == nullptr || pLayout->count() == 0 ) {
Still seeing the same thing.
@SPlatten said in Correct way to remove content from layout?:
QFormLayout::takeAt: Invalid index 0
Well, on last iteration you will get this warning as there is nothing to take from the layout anymore. Replace your while() loop with a for().
-
@raven-worx thank you, just tried that, same result.
@SPlatten what about
while( pLayout->count() ) { QLayoutItem* pItem = pLayout->takeAt(0); if ( QWidget* pWidget = pItem->widget() ) { pWidget->deleteLater(); } else if ( QLayout* pChildLayout = pItem->layout() ) { removeContent(pChildLayout); pChildLayout->deleteLater(); } }
-
Hi
I was wondering if
while( pLayout->count && QLayoutItem* pItem = pLayout->takeAt(0) )
would do it ?
ps. I like @J-Hilk version better as we don't have to rely on lazy evaluation from the compiler
-
@SPlatten what about
while( pLayout->count() ) { QLayoutItem* pItem = pLayout->takeAt(0); if ( QWidget* pWidget = pItem->widget() ) { pWidget->deleteLater(); } else if ( QLayout* pChildLayout = pItem->layout() ) { removeContent(pChildLayout); pChildLayout->deleteLater(); } }