How to delete an already existing layout on a widget?

前端 未结 4 417
南笙
南笙 2021-01-04 06:53

You must first delete the existing layout manager (returned by layout()) before you can call setLayout() with the new layout.

4条回答
  •  难免孤独
    2021-01-04 07:14

    I want to remove the current layout, replace it with a new layout but keep all widgets managed by the layout. I found that in this case, Chris Wilson's solution does not work well. The layout is not always changed.

    This worked for me:

    void RemoveLayout (QWidget* widget)
    {
        QLayout* layout = widget->layout ();
        if (layout != 0)
        {
        QLayoutItem *item;
        while ((item = layout->takeAt(0)) != 0)
            layout->removeItem (item);
        delete layout;
        }
    }
    

提交回复
热议问题