Qt - Clear all widgets from inside a QWidget's layout

前端 未结 3 1374
悲&欢浪女
悲&欢浪女 2021-01-12 02:18

I have a QWidget in a dialog. Over the course of the program running, several QCheckBox * objects are added to the layout like this:



        
3条回答
  •  佛祖请我去吃肉
    2021-01-12 02:47

    Given that you have a widget hierarchy consisting of cascaded layouts containing widgets then you should better go for the following.

    Step 1: Delete all widgets

        QList< QWidget* > children;
        do
        {
           children = MYTOPWIDGET->findChildren< QWidget* >();
           if ( children.count() == 0 )
               break;
           delete children.at( 0 );
        }
        while ( true );
    

    Step 2: Delete all layouts

        if ( MYTOPWIDGET->layout() )
        {
            QLayoutItem* p_item;
            while ( ( p_item = MYTOPWIDGET->layout()->takeAt( 0 ) ) != nullptr )
                delete p_item;
            delete MYTOPWIDGET->layout();
        }
    

    After step 2 your MYTOPWIDGET should be clean.

提交回复
热议问题