I have a QMainWindow with an event filter installed. After I open and close a QDialog the keyboard arrow keys don't respond since QMainWindow receives only ShortcutOverride events instead of KeyPress events.
When I changed the QMainWindow's event filter to handle ShortcutOverride events I got a strange behavior since in my program each key press event is preceded by two shortcut override events (why??).
This doesn't work - events are handled more than once:
bool eventFilter(QObject *, QEvent *event) {
if(type == QEvent::KeyPress || type == QEvent::ShortcutOverride) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
switch(keyEvent->key()) {
case Qt::Key_Up:
case Qt::Key_Down:
case Qt::Key_Left:
case Qt::Key_Right:
// Do something here
break;
default:
break;
}
return true;
}
return false;
}
Why is this happening? Where do those ShortcutOverride events come from?
Edit:
As it turns out, QMainwindow loses focus when the QDialog is opened and never gains it back when the QDialog is closed. I used setFocus() in the eventFilter of QMainwindow and it seems to do the trick.