PDA

View Full Version : Raise window to top and give it focus



TonyInSoMD
8th August 2017, 12:33
I've got a program with 2 windows that are open. When I programmatically change the window that's on top, it doesn't have focus.


SecondWindow->raise();
SecondWindow->setFocus();

The second window comes to the top, but doesn't have focus
Since I'm working in windows if I add


HWND WinHandle = (HWND)SecondWindow->winID();
SetFocus(WinHandle);

It gets focus. Is there a way to do it using strictly Qt?

high_flyer
8th August 2017, 14:03
That the window does not get the focus is clear as the docs state:

Gives the keyboard input focus to this widget (or its focus proxy) if this widget or one of its parents is the active window.
And the reason you want to give the focus to the window is precisly because it is NOT the current active window.

Try calling QWidget::activateWindow() on the window you want to make active.

TonyInSoMD
8th August 2017, 14:15
I could have sworn I had already tried that and it didn't work, but it does now. Thanks.