FIX: use events to change style for lineEdit when we have input errors
On the current version of the reimplementation (commit 2594d852), the text color of lineEdit is changed to red when an validation error occurs, to indicate that something is wrong.
It's implemented by changing the lineEdit stylesheet, which is not a great practice:
def validate(self, text, position):
"""
TODO: write documentation
"""
#logging.debug(f'Incoming text: {self.removeSeparators(text)}')
pre_state, pre_text, position = self._inputValidator.validate(
self.removeSeparators(text),
position
)
if pre_state is self._inputValidator.State.Intermediate:
return pre_state, text, position
captured = self._inputValidator.getAllCaptured(text)
unit = captured['txt_unit']
logging.debug(f"{unit=}")
is_unit_empty = (not unit) and self._baseUnit
is_unit_invalid = not self.backend.isUnitRegistered(unit)
is_unit_incompatible_with_base = not self._backend.isUnitsCompatible(unit, self._baseUnit)
if is_unit_empty:
logging.debug('empty Unit event!')
self.emptyUnitEvent()
if is_unit_invalid and (not is_unit_empty):
self.invalidUnitEvent()
if is_unit_incompatible_with_base and (not is_unit_empty):
self.incompatibleUnitEvent()
if is_unit_invalid or is_unit_incompatible_with_base or is_unit_empty:
pre_state = self._inputValidator.State.Intermediate
else:
self.clearToolTip()
self.clearErrorColor()
#logging.debug(f"(state, text, position) = ({self._inputValidator.stateToStr(pre_state)}, {text}, {position})")
return pre_state, text, position
...
def setErrorColor(self):
self.lineEdit().setStyleSheet(
"""
QLineEdit#ScientificSpinBox_LineEdit {
color: red;
}
"""
)
def clearErrorColor(self):
self.lineEdit().setStyleSheet(
"""
QLineEdit#ScientificSpinBox_LineEdit {
color: black;
}
"""
)
...
We should fix this implementation and use Qt states instead, using something like QLineEdit#ScientificSpinBox_LineEdit[isError=True] on the stylesheet and then doing setProperty('isError', True) to set this state.