Calculadora: capturando teclas ENTER, Backspace e ESC
This commit is contained in:
@@ -63,7 +63,9 @@ class ButtonsGrid(QGridLayout):
|
|||||||
print('Signal recebido por "vouApagarVocê" em', type(self).__name__)
|
print('Signal recebido por "vouApagarVocê" em', type(self).__name__)
|
||||||
|
|
||||||
def _makeGrid(self):
|
def _makeGrid(self):
|
||||||
self.display.eqRequested.connect(self.vouApagarVocê)
|
self.display.eqPressed.connect(self.vouApagarVocê)
|
||||||
|
self.display.delPressed.connect(self.display.backspace)
|
||||||
|
self.display.clearPressed.connect(self.vouApagarVocê)
|
||||||
|
|
||||||
for rowNumber, rowData in enumerate(self._gridMask):
|
for rowNumber, rowData in enumerate(self._gridMask):
|
||||||
for colNumber, buttonText in enumerate(rowData):
|
for colNumber, buttonText in enumerate(rowData):
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
from PySide6.QtCore import Qt, Signal
|
from PySide6.QtCore import Qt, Signal
|
||||||
from PySide6.QtGui import QKeyEvent
|
from PySide6.QtGui import QKeyEvent
|
||||||
from PySide6.QtWidgets import QLineEdit
|
from PySide6.QtWidgets import QLineEdit
|
||||||
|
from utils import isEmpty
|
||||||
from variables import BIG_FONT_SIZE, MINIMUM_WIDTH, TEXT_MARGIN
|
from variables import BIG_FONT_SIZE, MINIMUM_WIDTH, TEXT_MARGIN
|
||||||
|
|
||||||
|
|
||||||
class Display(QLineEdit):
|
class Display(QLineEdit):
|
||||||
eqRequested = Signal()
|
eqPressed = Signal()
|
||||||
|
delPressed = Signal()
|
||||||
|
clearPressed = Signal()
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
@@ -20,12 +23,31 @@ class Display(QLineEdit):
|
|||||||
self.setTextMargins(*margins)
|
self.setTextMargins(*margins)
|
||||||
|
|
||||||
def keyPressEvent(self, event: QKeyEvent) -> None:
|
def keyPressEvent(self, event: QKeyEvent) -> None:
|
||||||
|
text = event.text().strip()
|
||||||
key = event.key()
|
key = event.key()
|
||||||
KEYS = Qt.Key
|
KEYS = Qt.Key
|
||||||
|
|
||||||
isEnter = key in [KEYS.Key_Enter, KEYS.Key_Return]
|
isEnter = key in [KEYS.Key_Enter, KEYS.Key_Return]
|
||||||
|
isDelete = key in [KEYS.Key_Backspace, KEYS.Key_Delete]
|
||||||
|
isEsc = key in [KEYS.Key_Escape]
|
||||||
|
|
||||||
if isEnter:
|
if isEnter:
|
||||||
print('Enter pressionado, sinal emitido', type(self).__name__)
|
print('Enter pressionado, sinal emitido', type(self).__name__)
|
||||||
self.eqRequested.emit()
|
self.eqPressed.emit()
|
||||||
return event.ignore()
|
return event.ignore()
|
||||||
|
|
||||||
|
if isDelete:
|
||||||
|
print('isDelete pressionado, sinal emitido', type(self).__name__)
|
||||||
|
self.delPressed.emit()
|
||||||
|
return event.ignore()
|
||||||
|
|
||||||
|
if isEsc:
|
||||||
|
print('isEsc pressionado, sinal emitido', type(self).__name__)
|
||||||
|
self.clearPressed.emit()
|
||||||
|
return event.ignore()
|
||||||
|
|
||||||
|
# Não passar daqui se não tiver texto
|
||||||
|
if isEmpty(text):
|
||||||
|
return event.ignore()
|
||||||
|
|
||||||
|
print('Texto', text)
|
||||||
|
|||||||
Reference in New Issue
Block a user