Python PyQt4-将按钮连接到函数-参数1具有意外类型,无法转换为QObject
我对python、PyQt4和图形一无所知。我希望能够单击某种按钮Python PyQt4-将按钮连接到函数-参数1具有意外类型,无法转换为QObject,python,button,connection,signals,pyqt4,Python,Button,Connection,Signals,Pyqt4,我对python、PyQt4和图形一无所知。我希望能够单击某种按钮play,开始一个新游戏,但我遇到了一些困难 以下是守则的相关部分: class GameWindow(QtGui.QGraphicsView): def __init__(self): super(GameWindow, self).__init__() self.scene = QtGui.QGraphicsScene() self.scene.setSceneRect
play
,开始一个新游戏,但我遇到了一些困难
以下是守则的相关部分:
class GameWindow(QtGui.QGraphicsView):
def __init__(self):
super(GameWindow, self).__init__()
self.scene = QtGui.QGraphicsScene()
self.scene.setSceneRect(0, 0, 470, 530)
self.view = QtGui.QGraphicsView(self.scene)
self.view.setFixedSize(470, 530)
def start(self):
pass
# some code about the new game
def main_menu(self):
playing = Button("Play")
bx_pos = self.view.width()/2 - playing.boundingRect().width()/2
by_pos = 275
playing.setPos(bx_pos, by_pos)
self.scene.addItem(playing)
self.connect(playing, QtCore.SIGNAL('clicked()'), self,
QtCore.SLOT('start()'))
我还发布了Button类的相关代码:
class Button(QtGui.QGraphicsRectItem):
def __init__(self, name):
super(Button, self).__init__()
#and some other inializating stuff
def mousePressEvent(self, event):
super(Button, self).mousePressEvent(event)
它给了我:
TypeError: arguments did not match any overloaded call:
QObject.connect(QObject, SIGNAL(), QObject, SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 1 has unexpected type 'Button'
QObject.connect(QObject, SIGNAL(), callable, Qt.ConnectionType=Qt.AutoConnection): argument 1 has unexpected type 'Button'
QObject.connect(QObject, SIGNAL(), SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 1 has unexpected type 'Button'
我读到我必须从QObject类继承来修复这个问题。我尝试将“类按钮(QtGui.qgraphicsrecitem)”更改为“类按钮(QtGui.qgraphicsrecitem,QtCore.QObject)”,但现在问题是
**TypeError: could not convert 'Button' to 'QObject'**
我读了一些关于多重继承的东西,我想这就是问题所在,但我不明白如何解决它,所以我决定在这里提问。
实际上,我不太确定应该如何定义mousePressEvent。我想这也可能是个问题。如果有人能帮助我,我将非常感激:)你为什么不简单地使用
QPushButton
?对于连接部分,您应该阅读def main_menu(self):btn=QtGui.QPushButton(“Play”,self)btn.clicked.connect(self.start)btn.resize(150,50)btn.move(220,170)self.scene.addItem(btn)
,如果我这样做,它会给我类型错误:qgraphicscenscene.addItem(qgraphicssitem):如果我只执行self.show()
而不是self.scene.addItem(btn)
,参数1具有意外的类型“QPushButton”,则该按钮会在新窗口中弹出,我希望它位于同一窗口中。不应将该按钮添加到图形视图中。您应该将按钮和图形视图添加到主窗口。这是一个教程,谢谢你的帮助。:)