PyQt4 - 将按钮连接到函数 - 参数1具有意外类型,无法转换为QObject

时间:2015-08-28 19:04:11

标签: 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(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类继承来解决这个问题。我尝试通过将“类Button(QtGui.QGraphicsRectItem)”更改为“类Button(QtGui.QGraphicsRectItem,QtCore.QObject)”来实现它,但现在问题是

**TypeError: could not convert 'Button' to 'QObject'**

我读了一些关于多重继承的东西,我想这是问题所在,但我无法理解如何修复它,所以我决定在这里问一下。 其实我不太确定应该如何定义mousePressEvent。我想这也可能是一个问题。如果有人可以帮助我,我将非常感激:)

0 个答案:

没有答案