Calculadora: keyPressEvent do QLineEdit e criando um Signal

This commit is contained in:
Luiz Otávio
2023-03-03 15:40:04 -03:00
parent 599211a542
commit 8afa43dc61
2 changed files with 22 additions and 3 deletions

View File

@@ -59,7 +59,12 @@ class ButtonsGrid(QGridLayout):
self._equation = value self._equation = value
self.info.setText(value) self.info.setText(value)
def vouApagarVocê(self):
print('Signal recebido por "vouApagarVocê" em', type(self).__name__)
def _makeGrid(self): def _makeGrid(self):
self.display.eqRequested.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):
button = Button(buttonText) button = Button(buttonText)
@@ -81,7 +86,7 @@ class ButtonsGrid(QGridLayout):
if text == 'C': if text == 'C':
self._connectButtonClicked(button, self._clear) self._connectButtonClicked(button, self._clear)
if text in 'D': if text == 'D':
self._connectButtonClicked(button, self.display.backspace) self._connectButtonClicked(button, self.display.backspace)
if text in '+-/*^': if text in '+-/*^':
@@ -90,7 +95,7 @@ class ButtonsGrid(QGridLayout):
self._makeSlot(self._operatorClicked, button) self._makeSlot(self._operatorClicked, button)
) )
if text in '=': if text == '=':
self._connectButtonClicked(button, self._eq) self._connectButtonClicked(button, self._eq)
def _makeSlot(self, func, *args, **kwargs): def _makeSlot(self, func, *args, **kwargs):

View File

@@ -1,9 +1,12 @@
from PySide6.QtCore import Qt from PySide6.QtCore import Qt, Signal
from PySide6.QtGui import QKeyEvent
from PySide6.QtWidgets import QLineEdit from PySide6.QtWidgets import QLineEdit
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()
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.configStyle() self.configStyle()
@@ -15,3 +18,14 @@ class Display(QLineEdit):
self.setMinimumWidth(MINIMUM_WIDTH) self.setMinimumWidth(MINIMUM_WIDTH)
self.setAlignment(Qt.AlignmentFlag.AlignRight) self.setAlignment(Qt.AlignmentFlag.AlignRight)
self.setTextMargins(*margins) self.setTextMargins(*margins)
def keyPressEvent(self, event: QKeyEvent) -> None:
key = event.key()
KEYS = Qt.Key
isEnter = key in [KEYS.Key_Enter, KEYS.Key_Return]
if isEnter:
print('Enter pressionado, sinal emitido', type(self).__name__)
self.eqRequested.emit()
return event.ignore()