I have an issue with associating a keypress to a QAction. I’m mapping Enter to an action like this:
myAction->setShortcut(Qt::Key_Return);
The problem is that the QAction is also triggered when I type a value in a QSpinBox and then press Enter. I was expecting the spinbox to consume the event (so it’s not caught by the mainwindow) but it’s not working like that.
I’ve seen that I can add a context to my shortcut, I tried all values but this does not solve my issue.
@Matthew It is indeed rejecting the event:
case Qt::Key_Enter:
case Qt::Key_Return:
d->edit->d_func()->control->clearUndo();
d->interpret(d->keyboardTracking ? AlwaysEmit : EmitIfChanged);
selectAll();
event->ignore();
emit editingFinished();
return;
I find this behavior quite strange.
From my knowledge the
QSpinBoxdefault behavior does wait until you press return to accept the value. Technically the value is set/accepted as soon as you change it.If you’re looking to change that behaviour, i.e. when you hit return on your
QSpinBoxyou the value is actually set/accepted, then you will most likely have to handle the associated key press event yourself on theQSpinBoxand mark it as accepted (i.e.event->accepted()).Otherwise, this sounds like it is working as originally designed although not as you want.