Qt - Focus on a application that has lost focus?

核能气质少年 提交于 2019-12-23 12:09:48

问题


I would like to know if it's possible to focus on my application even if it has lost focus?

My application is running a thread, and once I exit it, I would like to bring my window application to the front.
I've tried the following code, it works to focus on the widget I'd like to, but if you're on firefox, it will just flash once in taskbar, and don't focus :(

this->activateWindow();
this->show();
this->setFocus();

EDIT: This would do it, but I don't want my application stay on top... and if I remove the flag, it loses focus =/

this->setWindowFlags(Qt::WindowStaysOnTopHint);
this->activateWindow();
this->show();
this->setFocus();

Thank you in advance for your answers.


回答1:


try

this->setWindowState(Qt::WindowActive);

it worked for me. (Qt 4.8, Windows 7, MinGW 4.4)

EDIT: I've since found that this usually only works if the window is currently minimized. so if it is not working try adding

this->setWindowState(Qt::WindowMinimized);

before it.



来源:https://stackoverflow.com/questions/6230557/qt-focus-on-a-application-that-has-lost-focus

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!