Get a layout's widgets in PyQT

后端 未结 2 2062
甜味超标
甜味超标 2021-01-11 15:34

I have a QVBoxLayout that I\'ve added a few widgets to, via addWidget(). I need to now delete those widgets, and it seems I need to use remo

相关标签:
2条回答
  • 2021-01-11 16:07

    That's odd. My understanding is that adding widgets via addWidget transfers ownership to the layout so calling children() ought to work.

    However, as an alternative you could loop over the layout items by using count() and itemAt(int) to supply a QLayoutItem to removeItem(QLayoutItem*).

    Edit:

    I've just tried addWidget with a straight C++ test app. and it doesn't transfer QObject ownership to the layout so children() is indeed an empty list. The docs clearly say that ownership is transferred though...

    Edit 2:

    Okay, it looks as though it transfers ownership to the widget that has that layout (which is not what the docs said). That makes the items in the layout siblings of the layout itself in the QObject hierarchy! It's therefore easier to stick with count and itemAt.

    0 讨论(0)
  • 2021-01-11 16:11

    To get a widget from a QLayout, you have to call its itemAt(index) method. As the name of this method implies, it will return an item instead of a widget. Calling widget() on the result will finally give you the widget:

    myWidget = self.myLayout.itemAt(index).widget()
    

    To remove a widget, set the parent widget to None:

    myWidget.setParent(None)
    

    Also really helpfull is the QLayout count() method. To find and delete all contents of a layout:

    index = myLayout.count()
    while(index >= 0):
        myWidget = myLayout.itemAt(index).widget()
        myWidget.setParent(None)
        index -=1
    
    0 讨论(0)
提交回复
热议问题