How To Implement A Key Listener In Pyqt5 For A Qplaintextedit (or Any Other Component)
While I realize others have asked similar questions, my limited knowledge seems to have resulted in a missing piece to this puzzle. I will further explain why this question hasn't
Solution 1:
Qt Designer provides a simple method to create a GUI, but it only implements the design and not the logic, the task you want to do is part of the logic. For this it is advisable to create a new class that uses the previous design.
To do this you must create a new file that will call you main.py which must be in the same folder as the file generated by Qt Designer. To the file generated by Qt Designer I will call it design.py:
.
├── design.py
└── main.py
This class inherits from the window that you used as a template: QMainWindow and the generated design, you must call setupUi() to fill the widgets.
from PyQt5 import QtCore, QtGui, QtWidgets
from design import Ui_MainWindow
classMainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def__init__(self, *args, **kwargs):
QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
defkeyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Escape:
self.close()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Post a Comment for "How To Implement A Key Listener In Pyqt5 For A Qplaintextedit (or Any Other Component)"