Skip to content

Commit

Permalink
Merge pull request #18 from jnsebgosselin/work_around_dspinbox
Browse files Browse the repository at this point in the history
PR: consume Enter or Return key press and release events in QDoubleSpinBox
  • Loading branch information
jnsebgosselin authored Jun 25, 2024
2 parents cf661fb + 6523af9 commit 30c7dcd
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion qtapputils/widgets/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# -----------------------------------------------------------------------------

# ---- Third party imports
from qtpy.QtCore import Signal, QObject, QLocale
from qtpy.QtCore import Signal, QObject, QLocale, Qt
from qtpy.QtGui import QValidator
from qtpy.QtWidgets import QDoubleSpinBox, QWidget

Expand All @@ -21,6 +21,33 @@ class DoubleSpinBox(QDoubleSpinBox):
the bug described at https://bugreports.qt.io/browse/QTBUG-77939.
"""

def __init__(self, parent: QWidget = None,
consume_enter_events: bool = True):
super().__init__(parent)
# Whether to consume key press and key release events.
# See jnsebgosselin/qtapputils#18
self.consume_enter_events = consume_enter_events

def keyReleaseEvent(self, event):
"""
Override qt base method to consume key release events so that they are
not propagated to the parent.
"""
super().keyReleaseEvent(event)
if (event.key() in (Qt.Key_Enter, Qt.Key_Return) and
self.consume_enter_events):
event.accept()

def keyPressEvent(self, event):
"""
Override qt base method to consume key press events so that they are
not propagated to the parent.
"""
super().keyPressEvent(event)
if (event.key() in (Qt.Key_Enter, Qt.Key_Return) and
self.consume_enter_events):
event.accept()

def textFromValue(self, value: float):
"""
Override qt base method to work around the bug described at
Expand Down

0 comments on commit 30c7dcd

Please sign in to comment.