Calculadora: keyPressEvent do QLineEdit e criando um Signal
This commit is contained in:
@@ -59,7 +59,12 @@ class ButtonsGrid(QGridLayout):
|
||||
self._equation = value
|
||||
self.info.setText(value)
|
||||
|
||||
def vouApagarVocê(self):
|
||||
print('Signal recebido por "vouApagarVocê" em', type(self).__name__)
|
||||
|
||||
def _makeGrid(self):
|
||||
self.display.eqRequested.connect(self.vouApagarVocê)
|
||||
|
||||
for rowNumber, rowData in enumerate(self._gridMask):
|
||||
for colNumber, buttonText in enumerate(rowData):
|
||||
button = Button(buttonText)
|
||||
@@ -81,7 +86,7 @@ class ButtonsGrid(QGridLayout):
|
||||
if text == 'C':
|
||||
self._connectButtonClicked(button, self._clear)
|
||||
|
||||
if text in 'D':
|
||||
if text == 'D':
|
||||
self._connectButtonClicked(button, self.display.backspace)
|
||||
|
||||
if text in '+-/*^':
|
||||
@@ -90,7 +95,7 @@ class ButtonsGrid(QGridLayout):
|
||||
self._makeSlot(self._operatorClicked, button)
|
||||
)
|
||||
|
||||
if text in '=':
|
||||
if text == '=':
|
||||
self._connectButtonClicked(button, self._eq)
|
||||
|
||||
def _makeSlot(self, func, *args, **kwargs):
|
||||
|
||||
@@ -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 variables import BIG_FONT_SIZE, MINIMUM_WIDTH, TEXT_MARGIN
|
||||
|
||||
|
||||
class Display(QLineEdit):
|
||||
eqRequested = Signal()
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.configStyle()
|
||||
@@ -15,3 +18,14 @@ class Display(QLineEdit):
|
||||
self.setMinimumWidth(MINIMUM_WIDTH)
|
||||
self.setAlignment(Qt.AlignmentFlag.AlignRight)
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user