88# -----------------------------------------------------------------------------
99
1010# ---- Third party imports
11- from qtpy .QtCore import Signal , QObject , QLocale
11+ from qtpy .QtCore import Signal , QObject , QLocale , Qt
1212from qtpy .QtGui import QValidator
1313from qtpy .QtWidgets import QDoubleSpinBox , QWidget
1414
@@ -21,6 +21,33 @@ class DoubleSpinBox(QDoubleSpinBox):
2121 the bug described at https://bugreports.qt.io/browse/QTBUG-77939.
2222 """
2323
24+ def __init__ (self , parent : QWidget = None ,
25+ consume_enter_events : bool = True ):
26+ super ().__init__ (parent )
27+ # Whether to consume key press and key release events.
28+ # See jnsebgosselin/qtapputils#18
29+ self .consume_enter_events = consume_enter_events
30+
31+ def keyReleaseEvent (self , event ):
32+ """
33+ Override qt base method to consume key release events so that they are
34+ not propagated to the parent.
35+ """
36+ super ().keyReleaseEvent (event )
37+ if (event .key () in (Qt .Key_Enter , Qt .Key_Return ) and
38+ self .consume_enter_events ):
39+ event .accept ()
40+
41+ def keyPressEvent (self , event ):
42+ """
43+ Override qt base method to consume key press events so that they are
44+ not propagated to the parent.
45+ """
46+ super ().keyPressEvent (event )
47+ if (event .key () in (Qt .Key_Enter , Qt .Key_Return ) and
48+ self .consume_enter_events ):
49+ event .accept ()
50+
2451 def textFromValue (self , value : float ):
2552 """
2653 Override qt base method to work around the bug described at
0 commit comments