Usando eventFilter e installEventFilter com UI do Qt Designer
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user