Key press (arrow keys) cause loss of focus
-
@Axel-Spoerl QT version 6.6.1: Here's the call stack at the time the event filter receives that FocusOut event:
It's not at all clear to me why it is doing that.
If there's information you need me to dig out from that call stack please let me know.
David
-
@Perdrix
Hi David,the gui event dispatcher sends posted events, which makes it forward window system events. The first of those causes a focus change. It doesn't result from the application's main thread, because we don't see any
postEvent()
in the call stack. AFAIK, that can have two possible reasons:- The window manager decides, that another application gets focus. It can be anything from the debugger to a system popup or a logging window receiving output to display.
- The focus widget disappears (it gets hidden or destroyed) and the focus chain is broken, which is why the next focus widget can't be established.
It would be helpful to know the values of
QWidget * focus, Qt::FocusReason reason
in Line 1539 - 6 lines down from the break point. The widget will probably benullptr
, because that's where we end up. The focus reason will not tell us what exactly is going on, but it will tell us why the focus was changed. -
@Axel-Spoerl I put this in my Event Logging code:
case QEvent::FocusOut: inputType = FOCUS; eventType = "FocusOut"; if (tableView == obj) __debugbreak(); break;
When the breakpoint is hit I think the relevant part of the call stack is:
where the focus change is being driven from QGuiApplicationPrivate::processActivatedEvent() at line 2562. At that point in the code, newFocus is a null pointer and previousFocusObject -> my table view object.
So why is the code forcing a focus change?
David
-
@Perdrix
That baffles me. Can you step into Qt code?
Or isolate the issue in a minimal reproducer? -
@Axel-Spoerl I can step into and put breakpoints into the Qt code.
Please tell me where you want the breakpoints and what information you need
-
@Perdrix
If you can compile Qt, throw aqDebug() << __FUNCTION__ << type
into the c'tor ofQFocusEvent
(qevent.cpp:1562).
Set a break point there and continue, unless it's aQEvent::FocusOut
. If it's theFocusOut
, that eventually steals focus, the call stack will tell who it is. -
The reason in the focus event is 3 (Qt::ActiveWindowFocusReason).
This was invoked indirectly from line 1940 in QApplicationPrivate::notifyActiveWindowChange()
David
-
@Perdrix said in Key press (arrow keys) cause loss of focus:
The reason in the focus event is 3 (Qt::ActiveWindowFocusReason).
That's interesting. What's the
QWidget *focus
argument pointing to?
Shouldn't be nullptr withActiveWindowFocusReason
... -
@Axel-Spoerl It's pointing to a QScrollArea object (which could be the scroll area in the other dock widget)
-
@Perdrix
Getting more and more interesting.
Could you assign aQObject::objectName()
to the suspicious scroll area? That will become visible in the debugger, so you can easily identify which one it is. Looks like the arrow key event gets delivered to another dock widget, which then consumes the event in an attempt to scroll. Eventually it gets focus as a stray bullet. -
@Axel-Spoerl The variable focusWidget set at line 1937 in QApplicationPrivate::notifyActiveWindowChange() points to the QMainWindow derived class for the application. The code then calls setActiveWindow() for that.
By the time this all gets to the actual Event Filter code the object to which focus is being given is the scroll area that belongs to the "other" dock widget.
I can recreate the problem without use of arrow keys - just clicking on a row in the table view selects it and starts the loading of the image. Just after the loading of the image completes, the focus is lost. The code that executes in my application on completion of loading the image was posted earlier in this thread.
-
@Axel-Spoerl I found the problem!!!
The code that was called on completion of loading of the image contained the following:
if (pToolBar->rectAction->isChecked()) { editStars->rectButtonPressed(); selectRect->rectButtonPressed(); } else if (pToolBar->starsAction->isChecked()) { editStars->starsButtonPressed(); selectRect->starsButtonPressed(); } else if (pToolBar->cometAction->isChecked()) { editStars->cometButtonPressed(); selectRect->cometButtonPressed(); }
each of the xxxxButtonPressed() member functions called activateWindow(); which in the end resulted in the table view losing focus. I removed the activateWindow(); calls and now it works as it should.
Thank you so much for your help - without it I would probably still be struggling with this for many weeks to come.
-
-
@Perdrix
Hi David,
glad that the issue is resolved!
Thanks for letting me know - was a pleasure.
Cheers
Axel