void OTS_MainWindow::DeleteAllWidgets(QLayout *layout) { QLayoutItem *item; while ((item=layout->takeAt(0)) !=0) { QWidget *w = item->widget(); delete item; layout->removeWidget(w); delete w; } }
void OTS_MainWindow::DeleteAllLayouts(QLayout *layout){ if (layout->count()==0) DeleteAllWidgets(layout); else { int size=layout->count(); while (size!=0) { QLayout *SubLayout=static_cast<QLayout*>(layout->itemAt(size-1)); DeleteAllLayouts(SubLayout); --size; } }}
C++ (Qt)QWidget *w = item->widget();layout->removeWidget(w);delete w;delete item;
C++ (Qt)#include <QApplication>#include <QGridLayout>#include <QPushButton>#include <QTest> int main( int argc, char** argv ){ QApplication app( argc, argv ); QWidget w; while( 1 ) { QTest::qWait( 500 ); QGridLayout* l = new QGridLayout; for( int i = 0; i < 10; i++ ) for( int j = 0; j < 10; j++ ) l->addWidget( new QPushButton( QString( "%1:%2" ).arg( i ).arg( j ) ), i, j ); w.setLayout( l ); if( !w.isVisible() ) w.show(); QTest::qWait( 500 ); qDeleteAll( w.children() ); } return 0;}
C++ (Qt)#include <QApplication>#include <QGridLayout>#include <QPushButton>#include <QTabWidget>#include <QTest> int main( int argc, char** argv ){ QApplication app( argc, argv ); QTabWidget tw; QWidget w; bool not_shown = true; tw.addTab( &w, "Tab" ); while( 1 ) { tw.setTabText( 0, QString( "Tab (%1 children )" ).arg( w.children().count() ) ); QTest::qWait( 500 ); QGridLayout* l = new QGridLayout( &w ); for( int i = 0; i < 5; i++ ) for( int j = 0; j < 5; j++ ) l->addWidget( new QPushButton( QString( "%1:%2" ).arg( i ).arg( j ) ), i, j ); tw.setTabText( 0, QString( "Tab (%1 children )" ).arg( w.children().count() ) ); if( not_shown ) { not_shown = false; tw.show(); } QTest::qWait( 1000 ); qDeleteAll( w.children() ); if( tw.isHidden() ) break; } return 0;}