PyQt5 - 无法解析对话框的插槽

时间:2016-01-17 23:25:44

标签: python qt pyqt

我正在尝试使用PyQt5将一个小型Qt C ++应用程序移植到Python,但我现在遇到了最基本的问题。
我已经使用pyuic实用程序将.ui文件转换为相应的.py文件,该文件表示对话框的静态定义UI和两个信号/插槽对(两个按钮)。确切的错误是“'QDialog'对象没有属性'pushButtonStartClick'”,虽然我理解它描述的是什么,但我不知道如何解决它。我甚至不确定Ui类中是否可以定义自定义插槽。如果这是真的,它代表了一个非常巨大的限制。我在Windows机器上使用PyQt 5.4和Python 2.7。

其他信息 - 简单tutorial我一直在阅读并试图模仿。

提前谢谢。

包含UI类的生成的.py文件(部分文件,源.ui文件为here):

class Ui_NewGameDialog(object):
    def setupUi(self, NewGameDialog):
        NewGameDialog.setObjectName("NewGameDialog")
        NewGameDialog.setWindowModality(QtCore.Qt.ApplicationModal)
        NewGameDialog.resize(265, 200)
        #
        # ...
        #
        self.retranslateUi(NewGameDialog)
        # part where it breaks apart
        self.pushButtonStart.clicked.connect(NewGameDialog.pushButtonStartClick)
        self.pushButtonCancel.clicked.connect(NewGameDialog.pushButtonCancelClick)
        QtCore.QMetaObject.connectSlotsByName(NewGameDialog)

继承它的类:

class NewGameDialog(Ui_NewGameDialog):
    def __init__(self, dialog):
        Ui_NewGameDialog.__init__(self)
        self.setupUi(dialog)
        dialog.setFixedSize(dialog.size())
        dialog.setWindowFlags(
                dialog.windowFlags() ^ QtCore.Qt.WindowContextHelpButtonHint
        )

    @pyqtSlot(name="pushButtonStartClick")
    def pushButtonStartClick(self):
        pass

    @pyqtSlot(name="pushButtonCancelClick")
    def pushButtonCancelClick(self):
        pass

初​​始化:

if __name__ == "__main__":
    app = QApplication(sys.argv)

    dialog = QtWidgets.QDialog()
    ui = NewGameDialog(dialog)
    dialog.show()

    sys.exit(app.exec_())

2 个答案:

答案 0 :(得分:1)

我认为setupUi方法得到了一种错误的对象,因为QDialog没有那些pushButtonStartClickpushButtonCancelClick

class NewGameDialog(QtGui.QDialog, Ui_NewGameDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        Ui_NewGameDialog.__init__(self)
        self.setupUi(self)
        self.setFixedSize(self.size())
        self.setWindowFlags(
            self.windowFlags() ^ QtCore.Qt.WindowContextHelpButtonHint
        )

    @pyqtSlot(name="pushButtonStartClick")
    def pushButtonStartClick(self):
        pass

    @pyqtSlot(name="pushButtonCancelClick")
    def pushButtonCancelClick(self):
        pass

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)

    dialog = NewGameDialog()
    dialog.show()

    sys.exit(app.exec_())

没有多重继承:

class NewGameDialog(QtGui.QDialog):
    def __init__(self):
        super(NewGameDialog, self).__init__()
        self.ui = Ui_NewGameDialog()
        self.ui.setupUi(self)
...

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    dialog = NewGameDialog()

答案 1 :(得分:1)

@JJHakala(使用多重继承)的答案是PyQt documentation中的选项3。选项2也适合您的情况(这是J. J. Hakala的编辑中提供的“无多重继承”选项稍微更优雅的版本,基于PyQt文档中的示例):

class NewGameDialog(QtWidgets.QDialog):
    def __init__(self):
        QtWidgets.QDialog.__init__(self)
        self.ui = Ui_NewGameDialog()
        self.ui.setupUi(self)
        self.setFixedSize(self.size())
        self.setWindowFlags(
                self.windowFlags() ^ QtCore.Qt.WindowContextHelpButtonHint
        )

    @pyqtSlot(name="pushButtonStartClick")
    def pushButtonStartClick(self):
        pass

    @pyqtSlot(name="pushButtonCancelClick")
    def pushButtonCancelClick(self):
        pass

if __name__ == "__main__":
    app = QApplication(sys.argv)

    dialog = NewGameDialog()
    dialog.show()

    sys.exit(app.exec_())