I'm working on an existing project and I have very little experience with PyQt so I'm not sure what I would need to produce a minimal working example here, maybe someone can help me out anyways.
There is a part in the code where a QPushButton
is created and added to a QGraphicsScene
, like this:
b = QPushButton('foo')
scene.addWidget(b)
I'd like to replace b
with a class inheriting from QPushButton
, something like:
class Bar(QPushButton):
def __init__(self):
super(Bar, self).__init__('foo')
I believe that the following should then behave identical to the first code sample:
b = Bar()
scene.addWidget(b)
Instead the code crashes here and I have so far not been able to figure out why. Is there something obvious I'm doing wrong?
Try it:
class Bar(QPushButton):
def __init__(self, button):
super(Bar, self).__init__()
self.setText(button)
self.clicked.connect(lambda: print("Press button"))
class Desktop(QMainWindow):
def __init__(self):
super(Desktop, self).__init__()
self.centerWidget = QWidget(self)
layout = QVBoxLayout()
scene = QGraphicsScene()
view = QGraphicsView(scene)
b = Bar("Press me :)")
scene.addWidget(b)
layout.addWidget(view)
self.centerWidget.setLayout(layout)
self.setCentralWidget(self.centerWidget)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Desktop()
ex.show()
sys.exit(app.exec_())