If I run this code:
#!/usr/local/bin/ python3 import sys from PyQt4.QtCore import * from PyQt4.QtGui import * class Window(QMainWindow): def __init__(self): super().__init__() self.button1 = QPushButton("1") self.button2 = QPushButton("2") self.setCentralWidget(self.button1) self.button1.clicked.connect(lambda: self.setCentralWidget(self.button2)) self.button2.clicked.connect(lambda: self.setCentralWidget(self.button1)) self.show() if __name__ == '__main__': import sys app = QApplication(sys.argv) window = Window() sys.exit(app.exec_())
... I get this output:
Traceback (most recent call last): File "test.py", line 16, in <lambda> self.button2.clicked.connect(lambda: self.setCentralWidget(self.button1)) RuntimeError: wrapped C/C++ object of type QPushButton has been deleted
I do not understand why the object is being deleted. The window should contain a link to it. I carefully studied these posts: It is clear that the error "the underlying C / C ++ object has been removed" Can I query QQbject PyQt4 to determine if the original C ++ instance was damaged?
Why is the button deleted?
garbage-collection pyqt pyqt4 qobject qmainwindow
Brian
source share