PyQt5 keyPressEvent 如何工作 [英] How PyQt5 keyPressEvent works

查看:68
本文介绍了PyQt5 keyPressEvent 如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 PyQt GPL v5.4 创建了一个 UI,并使用 pyuic5 将 *.ui 文件转换为 *.py

I create a UI from PyQt GPL v5.4 and use pyuic5 convert *.ui file to *.py

但我不知道 keyPressEvent 在这段代码中是如何工作的!!

But I do not know how keyPressEvent work in this code!!

它应该适用于 QWidget,但如何让它工作.

It should work for QWidget, but how to let it works.

请帮忙!

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget

class Ui_MainWindow(QWidget,object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(200, 200)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(50, 110, 75, 23))
        self.pushButton.setObjectName("pushButton")


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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))\

    def keyPressEvent(self, e):
        if e.key() == Qt.Key_F5:
            self.close()


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

推荐答案

在开始我的回答之前,建议不要修改生成 Qt Designer 的类,在您的情况下,我认为您使用了模板 MainWindow,在以下代码我添加了一些您已删除的代码,您必须做的是创建一个实现生成视图的新类:

A recommendation before starting my answer, do not modify the class that generates Qt Designer, in your case by the name I think you used the template MainWindow, in the following code I added a bit of code that you have removed, what you must do is Create a new class that implements the generated view:

查看:

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(200, 200)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.setCentralWidget(self.centralwidget)
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(50, 110, 75, 23))
        self.pushButton.setObjectName("pushButton")


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

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

实现视图的类必须从模板的类继承,在您的情况下为 QMainWindow,并且除了调用父构造函数外,还使用 ​​setupUI 方法,即在您的情况下为 QMainWindow.

The class that implements the view must inherit from the class of the template, in your case of QMainWindow, and use the setupUI method in addition to calling the parent constructor, ie in your case of QMainWindow.

逻辑:

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent=parent)
        self.setupUi(self)

    def keyPressEvent(self, e):
        if e.key() == Qt.Key_F5:
            self.close()

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

通过这些修改,keyPressEvent 方法已经可以工作了.

With those modifications the keyPressEvent method already works.

这篇关于PyQt5 keyPressEvent 如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆