简体   繁体   中英

PyQt 5 keyPressEvent doesn`t work for terminating App Qt Designer

Good evening everyone! I`m just starting using pyQt5 and Qt Designer in Python so met some troubles while coding the example below. No matter about buttons and so on inside the App, the question is about using keyPressEvent ie for terminating App by Esc button. There are 2 files: one generated by designer, second one - where the keyPressEvent is. Form is opening, but nothing happened to it pressing Esc butt. Looking forward for your hepl! Thanks.

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_())

You must use the eventFilter method, in the constructor:

qApp.installEventFilter(self)

Then we override the method:

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)

Complete code:

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_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM