0

ガベージコレクション/適切なクリーンアップの観点から、私が持っているものは次のとおりです。

class MyWidget(QWidget):
    def __init__(self,qtParent):
        QWidget.__init__(self,qtParent):
        
        self.mySubWidget = MySubWidget(self)  # <-- keeping a direct reference to the child

MyWidget を破棄すると、以下のコードを呼び出すと、mySubWidget も Qt/pyside/python によって正しく破棄されますか?

setAttribute( Qt.DeleteOnClose, True)
myWidget.close()

または、以下のようなweakrefsを使用する必要がありますか?

import weakref

class MyWidget(QWidget):
    def __init__(self,qtParent):
        QWidget.__init__(self,qtParent):
        
        self.mySubWidget = weakref.ref(MySubWidget(self))
4

1 に答える 1

2

The first one is fine. If you don't have any other reference to the created MySubWidget outside of your instance, then it will be garbage collected when you delete the MyWidget instance.

于 2012-04-13T22:05:21.420 に答える