C++ 将窗口放在前面 -> raise(),show(),activateWindow() 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6087887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bring window to front -> raise(),show(),activateWindow() don’t work
提问by Hedge
In my Qt-application I open a URL in the default-browser. Afterwards I want to bring the main-window of my application to the front again.
在我的 Qt 应用程序中,我在默认浏览器中打开了一个 URL。之后我想再次将我的应用程序的主窗口放在前面。
I tried all approaches I could find but none worked. All it does is blink in the taskbar (of Window 7) Here's an example:
我尝试了我能找到的所有方法,但都没有奏效。它所做的只是在(Window 7 的)任务栏中闪烁,这是一个示例:
this->viewer->show();
this->viewer->raise();
this->viewer->activateWindow();
*viewer is a pointer to a QmlApplicationViewer which is derived from QDeclarativeView
*viewer 是一个指向从 QDeclarativeView 派生的 QmlApplicationViewer 的指针
采纳答案by Johan R?de
This problem is specific to Windows. If the active window belongs to some process, then Windows does not allow other processes to change the active Window.
此问题特定于 Windows。如果活动窗口属于某个进程,则 Windows 不允许其他进程更改活动窗口。
(Do not try the following: https://wiki.qt.io/Qt_project_org_faq#QWidget_::activateWindow.28.29_-_behavior_under_windows)
(不要尝试以下操作:https: //wiki.qt.io/Qt_project_org_faq#QWidget_:: activateWindow.28.29_-_ behavior_under_windows)
回答by iptton
try this:
尝试这个:
viewer.setWindowState( (windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
viewer.raise(); // for MacOS
viewer.activateWindow(); // for Windows
it work in my project ( in my project viewer is QMainWindow): https://github.com/iptton/Rythem.
它在我的项目中工作(在我的项目查看器中是 QMainWindow):https: //github.com/iptton/Rythem。
回答by Sunburst275
I did it like this:
我是这样做的:
{
this->show(); // Restore from systray
this->setWindowState(Qt::WindowState::WindowActive); // Bring window to foreground
}
assuming "this
" is your QMainWindow
.
Worked like a charm.
假设“ this
”是您的QMainWindow
. 像魅力一样工作。
回答by Bruce Chidester
This issue is not specific to Windows....I have the same issue on Linux. My solution was to close() the window before I re open() it.
这个问题不是 Windows 特有的……我在 Linux 上也有同样的问题。我的解决方案是在我重新打开()之前关闭()窗口。
回答by agnu17
for ( QWindow* appWindow : qApplication.allWindows() )
{
appWindow->show(); //bring window to top on OSX
appWindow->raise(); //bring window from minimized state on OSX
appWindow->requestActivate(); //bring window to front/unminimize on windows
}
Note that this also brings up the window from other virtual desktops on both OSX and Windows. I did not test this on linux, it may work though.
请注意,这也会从 OSX 和 Windows 上的其他虚拟桌面调出窗口。我没有在 linux 上测试过这个,但它可能会工作。