简体   繁体   English

将窗口放在前面 -> raise(),show(),activateWindow() 不起作用

[英]Bring window to front -> raise(),show(),activateWindow() don’t work

In my Qt-application I open a URL in the default-browser.在我的 Qt 应用程序中,我在默认浏览器中打开了一个 URL。 Afterwards I want to bring the main-window of my application to the front again.之后我想再次将我的应用程序的主窗口放在前面。

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 的指针

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

This problem is specific to Windows.此问题特定于 Windows。 If the active window belongs to some process, then Windows does not allow other processes to change the active Window.如果活动窗口属于某个进程,则 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

I did it like this:我是这样做的:

{
 this->show(); // Restore from systray
 this->setWindowState(Qt::WindowState::WindowActive); // Bring window to foreground
}

assuming " this " is your QMainWindow .假设“ this ”是你的QMainWindow Worked like a charm.像魅力一样工作。

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.请注意,这也会从 OSX 和 Windows 上的其他虚拟桌面调出窗口。 I did not test this on linux, it may work though.我没有在 linux 上测试过这个,但它可能会工作。

This issue is not specific to Windows....I have the same issue on Linux.这个问题不是 Windows 特有的……我在 Linux 上也有同样的问题。 My solution was to close() the window before I re open() it.我的解决方案是在我重新打开()之前关闭()窗口。

For Windows I did it with WinAPI.对于 Windows,我用 WinAPI 做到了。 Also you need to know the window title;您还需要知道窗口标题;

#include <windows.h>
const QString windowTitle = "Some title";

HWND hwnd = ::FindWindowA(NULL, windowTitle.toLocal8Bit());
if (hwnd != NULL) {
    if (::IsWindowVisible(hwnd)) {
        ::SwitchToThisWindow(hwnd, TRUE);
    }
}
 

The following is borrowed from the forum and it works for me:以下是从论坛借来的,对我有用:

auto eFlags = viewer.windowFlags();
viewer.setWindowFlags(eFlags|Qt::WindowStaysOnTopHint);
viewer.show();
viewer.setWindowFlags(eFlags);
viewer.show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM