[SOLVED] help with keyPressEvent and focus
-
Hi,
I've overridden keyPressEvent in a MainWindow to trap the 4 arrow keys. If I click my mouse anywhere in an unpopulated area of the window to give it focus, then the 4 arrow keys are trapped and handled as desired.
However if I then click any of the buttons contained within the window, my arrow keys then start traversing the child controls, setting focus to each one through the tab order.
How can I keep it so that the window traps the arrow keys and they don't end up scrolling through the child controls?
Here is my handler, and I found it behaves the same regardless if I do or do not pass the event up to QMainWindow on the last line. As well, I call setFocusPolicy(Qt::StrongFocus); in the constructor.
@
void StageMotorsControlWin::keyPressEvent(QKeyEvent *keyevent)
{
if (keyevent->key()==Qt::Key_Left)
{
on_buttonXY_W_clicked();
}if (keyevent->key()==Qt::Key_Left) { on_buttonXY_W_clicked(); } if (keyevent->key()==Qt::Key_Up) { on_buttonXY_N_clicked(); } if (keyevent->key()==Qt::Key_Right) { on_buttonXY_E_clicked(); } if (keyevent->key()==Qt::Key_Down) { on_buttonXY_S_clicked(); } QMainWindow::keyPressEvent(keyevent); // with or without this it's not quite what I'd like.
}
@
Thanks!
-
What I have understood from your explanation is that.
Case #1. You have main window.
Case #2. You have area with few buttons.
Case #3. You have area without buttons.If you click on case #2, keys are taken by the buttons.
If you click on case#2, keys are handled properly.If that is the case, you can install the eventFilters on your buttons and filter out the arrow key events. You can look at the installEventFilter and eventFilter(...) methods of QObject.
-
Case #1. You have main window.
Case #2. You have area with few buttons.
Case #3. You have area without buttons.Yes, all true.
If I click anywhere in Case #3 (or MainWindow title bar) the MainWindow traps the arrow keys and does as needed - no buttons or other form elements gain focus (as desired)
If I click on any of the buttons in Case #2, that button takes focus. At that point, my arrow keys cause focus to traverse the buttons on the form as if I were pressing TAB on the keyboard.
I don't want arrows to ever "hit" the buttons, to be always trapped at the MainWindow level regardless if I've clicked any of the buttons on the form.
Really the only way is to put an event filter on all buttons? I can't trap it at the top level MainWindow?
Thanks
-
If you put the install filters for the button, your keys won't reach the buttons at all. Since you are telling that events reaching the buttons, they are handled then and there. They don't propagate to parent. You need to install the filters so that events reach the filter object first. You can make the MainWindows itself as the event filter the button. You can filter those events before reaching to buttons.
-
so I think you're saying that my overriding keyPressEvent(QKeyEvent *keyevent) within my MainWindow class, or even making an eventFilter at the main window level, will not work? I have to put an event filter on each button to prevent them from hitting
(I guess I was thinking that the event hits the main window first, and then goes to the children, but it sounds like it's the other way around) -
What I'm saying is that Install some QObject as event filter for Buttons. We have too much of english and imagination. It will really if you provide sample source code. We can make it work and give you.
-
Actually you are making sense. It all comes down to using an eventFilter rather than simply overriding the event in the window. I can work with this, thanks!