QLayout删除所有布局

Qt 的 QLayout 文档里是这么写的,但其实不完整,参看我最下面的代码。

[pure virtual] QLayoutItem *QLayout::takeAt(int index)
Must be implemented in subclasses to remove the layout item at index from the layout, and return the item. If there is no such item, the function must do nothing and return 0. Items are numbered consecutively from 0. If an item is removed, other items will be renumbered.
The following code fragment shows a safe way to remove all items from a layout:

-------------不完整的方式------------

QLayoutItem *child;
while ((child = layout->takeAt(0)) != 0) {
    ...
    delete child;
}

-----------正确方式--------

    QLayoutItem *child;
    while ((child = layout->takeAt(0)) != 0)
    {
        layout->removeWidget(child->widget());
        child->widget()->setParent(0);
        delete child;
    }
posted @ 2018-12-13 05:53  月满西楼  阅读(3162)  评论(0编辑  收藏  举报