replacing layout on a QWidget with another layout

前端 未结 1 1153
鱼传尺愫
鱼传尺愫 2020-12-09 11:29

I have a widget which changes when an option is toggled. This invalidates all layouts and widgets. I keep list of all layouts, so I can delete them using something similar t

相关标签:
1条回答
  • 2020-12-09 11:40

    You can simply reparent the layout to a temporary widget:

    def reLayout(self):
        QWidget().setLayout(self.layout())
        layout = QGridLayout(self)
        ...
    

    That will reparent all the child widgets to that temporary object, and that object is deleted immediately along with its new children because we don't keep a reference to it.

    But the typical way to have multiple layouts for a single widget and to be able to switch between them is to use a QStackedWidget or QStackedLayout.


    And if you still need the answer to that secondary question:

    How to delete the old layout first?

    It seems you can't delete directly a QObject which has a parent, because the parent is keeping a reference to that object. But you can add the object to a temporary QObjectCleanupHandler which, like the above solution, will be deleted with the object(s) it contains immediately:

    QObjectCleanupHandler().add(self.layout())
    
    0 讨论(0)
提交回复
热议问题