PyQt5 keyPressEvent 不适用于终止 App Qt Designer
Posted
技术标签:
【中文标题】PyQt5 keyPressEvent 不适用于终止 App Qt Designer【英文标题】:PyQt 5 keyPressEvent doesn`t work for terminating App Qt Designer 【发布时间】:2017-02-14 22:07:05 【问题描述】:大家晚上好! 我刚开始在 Python 中使用 pyQt5 和 Qt Designer,所以在编写下面的示例时遇到了一些麻烦。不管 App 内部的按钮等,问题都是关于使用 keyPressEvent 即通过 Esc 按钮终止 App。 有 2 个文件:一个由设计师生成,第二个 - 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】:您必须在构造函数中使用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_())
【讨论】:
以上是关于PyQt5 keyPressEvent 不适用于终止 App Qt Designer的主要内容,如果未能解决你的问题,请参考以下文章
pyinstaller 不适用于 pyqt5 和 matplotlib