From a04c0304e7e005eabf329a02a09ae8e24008e3a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Ot=C3=A1vio?= Date: Fri, 10 Mar 2023 09:39:45 -0300 Subject: [PATCH] Usando eventFilter e installEventFilter com UI do Qt Designer --- aula203-qtdesigner/src/main_window.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/aula203-qtdesigner/src/main_window.py b/aula203-qtdesigner/src/main_window.py index a9386df..a1d2c70 100644 --- a/aula203-qtdesigner/src/main_window.py +++ b/aula203-qtdesigner/src/main_window.py @@ -1,5 +1,8 @@ import sys +from typing import cast +from PySide6.QtCore import QEvent, QObject +from PySide6.QtGui import QKeyEvent from PySide6.QtWidgets import QApplication, QMainWindow from window import Ui_MainWindow @@ -11,10 +14,21 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.buttonSend.clicked.connect(self.changeLabelResult) # type: ignore + self.lineName.installEventFilter(self) + def changeLabelResult(self): text = self.lineName.text() self.labelResult.setText(text) + def eventFilter(self, watched: QObject, event: QEvent) -> bool: + if event.type() == QEvent.Type.KeyPress: + # Tenho certeza que o tipo é KeyPress + event = cast(QKeyEvent, event) + text = self.lineName.text() + self.labelResult.setText(text + event.text()) + + return super().eventFilter(watched, event) + if __name__ == '__main__': app = QApplication(sys.argv)