6

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

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

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

Please help!

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_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
chqbge
  • 73
  • 1
  • 1
  • 3
  • What part are you not clear on regarding how it works? Do you mean how object events are connected to slots? Do you mean how the event is routed by Qt to the slot (i.e. event handler)? Something else? – code_dredd Jul 25 '17 at 23:11

2 Answers2

8

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:

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"))

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.

logic:

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

With those modifications the keyPressEvent method already works.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
1

Just a general answer (for those who want general usecase):

import sys
from PyQt5.QtWidgets import (QApplication, QWidget)
from PyQt5.Qt import Qt

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        
    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Space:
            self.test_method()

    def test_method(self):
        print('Space key pressed')

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

    demo = MainWindow()
    demo.show()

    sys.exit(app.exec_())

Source: here

Scott
  • 4,974
  • 6
  • 35
  • 62