PyQt 5 keyPressEvent不能用于终止App Qt Designer

时间:2017-02-14 22:07:05

标签: python pyqt pyqt5 qt-designer

大家晚上好! 我刚开始在Python中使用 pyQt5 Qt Designer ,因此在编写下面的示例时遇到了一些麻烦。无论App内部的按钮等如何,问题都在于使用 keyPressEvent ,即通过Esc按钮终止App。 有两个文件:一个由设计师生成,第二个文件 - keyPressEvent 。 表单正在打开,但没有发生任何事情,按 Esc 对接。 期待你的呐喊! 感谢。

ui_main.py

from PyQt5 import QtCore, QtWidgets
from PyQt5 import QtQuickWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(400, 300)
        self.gridLayout = QtWidgets.QGridLayout(MainWindow)
        self.gridLayout.setObjectName("gridLayout")
        self.bigWindow = QtQuickWidgets.QQuickWidget(MainWindow)
        self.bigWindow.setResizeMode(QtQuickWidgets.QQuickWidget.SizeRootObjectToView)
        self.bigWindow.setObjectName("bigWindow")
        self.gridLayout.addWidget(self.bigWindow, 0, 0, 1, 1)
        self.dateTimeEdit = QtWidgets.QDateTimeEdit(MainWindow)
        self.dateTimeEdit.setObjectName("dateTimeEdit")
        self.gridLayout.addWidget(self.dateTimeEdit, 1, 0, 1, 1)
        self.progressBar = QtWidgets.QProgressBar(MainWindow)
        self.progressBar.setProperty("value", 24)
        self.progressBar.setObjectName("progressBar")
        self.gridLayout.addWidget(self.progressBar, 2, 0, 1, 1)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Dialog"))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QDialog()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

py_main.py

import sys
from PyQt5.QtWidgets import QDialog, QApplication
from PyQt5.QtCore import Qt
from ui_main import Ui_MainWindow


class MyMainWindow(QDialog, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MyMainWindow, self).__init__(parent)
        self.show()
        self.setupUi(self)


    def keyPressEvent(self, event):
            if event.key() == Qt.Key_Escape:
                self.close()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = MyMainWindow()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:1)

您必须在构造函数中使用eventFilter方法:

qApp.installEventFilter(self)

然后我们覆盖方法:

def eventFilter(self, obj, event):
    if event.type() == QEvent.KeyPress:
        if event.key() == Qt.Key_Escape:
            self.close()
    return super(MyMainWindow, self).eventFilter(obj, event)

完整代码:

import sys
from PyQt5.QtWidgets import QDialog, QApplication, qApp
from PyQt5.QtCore import Qt, QEvent
from ui_main import Ui_MainWindow

class MyMainWindow(QDialog, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MyMainWindow, self).__init__(parent)
        qApp.installEventFilter(self)
        self.setupUi(self)
        self.show()

    def eventFilter(self, obj, event):
        if event.type() == QEvent.KeyPress:
            if event.key() == Qt.Key_Escape:
                self.close()
        return super(MyMainWindow, self).eventFilter(obj, event)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = MyMainWindow()
    sys.exit(app.exec_())