Se corro questo codice:PyQt: RuntimeError: Spostato C/C++ oggetto è stato eliminato
#!/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_())
... ottengo questo 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
non capisco il motivo per cui l'oggetto è stato cancellato. La finestra dovrebbe mantenere un riferimento ad essa. Ho studiato a fondo questi post: Understanding the “underlying C/C++ object has been deleted” error Can a PyQt4 QObject be queried to determine if the underlying C++ instance has been destroyed?
Perché il pulsante di essere cancellati?
Avevo un problema simile con una sottoclasse di QWidget, e si è scoperto che il problema era che avevo dimenticato di chiamare '__init__' di QWidget all'inizio del mio' __init__'. Furbata. – spookypeanut