I try to build an application based on QDialog. It contains a 'Close' button, which always seems to go back into focus when I type into a QSpinBox.
QSpinBox.keyboardTracking = false
so I can process the final value.
Now clicking somewhere else or pressing enter within the spin box will actually accept the new value via the valueChanged
event.
But with enter, at the same time a 'click' is sent to the Close button.
I tried to override the keypress event:
class MyDoubleSpinBox(QDoubleSpinBox):
def __init__(self, *args, **kwargs):
super(MyDoubleSpinBox, self).__init__(*args, **kwargs)
def keyPressEvent(self, event):
key = event.key()
#Catch keys
if key == Qt.Key_Return or key == Qt.Key_Enter:
# Process current item here
print(self.value())
#self.nextInFocusChain().setFocus()
#import pdb;pdb.set_trace()
#self.valueChanged(self.value())
pass
else:
super(MyDoubleSpinBox, self).keyPressEvent(event)
But this will also disable the valueChanged event.
How can I prevent triggering the close button when editing a SpinBox without masking its valueChanged event as well?
EDIT: Or is QDialog the wrong class for the main window? :)
Thx @musicamante I changed my app from QDialog to QWidget. Everything looks the same, but actually works.