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

发布于 2024-11-09 03:59:31 字数 313 浏览 3 评论 0 原文

在我的 Qt 应用程序中,我在默认浏览器中打开一个 URL。 之后我想再次将应用程序的主窗口带到前面。

我尝试了所有能找到的方法,但没有一个有效。它所做的只是在(Windows 7 的)任务栏中闪烁 这是一个例子:

this->viewer->show();
this->viewer->raise();
this->viewer->activateWindow();

*viewer 是一个指向 QmlApplicationViewer 的指针,它派生自 QDeclarativeView

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.

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:

this->viewer->show();
this->viewer->raise();
this->viewer->activateWindow();

*viewer is a pointer to a QmlApplicationViewer which is derived from QDeclarativeView

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

如此安好 2024-11-16 03:59:31

试试这个:

viewer.setWindowState( (windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
viewer.raise();  // for MacOS
viewer.activateWindow(); // for Windows

它在我的项目中工作(在我的项目查看器中是 QMainWindow): https://github.com/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 .

就是爱搞怪 2024-11-16 03:59:31

此问题是 Windows 特有的。
如果活动窗口属于某个进程,那么Windows不允许其他进程更改活动窗口。

(不要尝试以下操作:
https://wiki.qt.io/Qt_project_org_faq#QWidget_::activateWindow。 28.29_-_windows下的行为)

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.

(Do not try the following:
https://wiki.qt.io/Qt_project_org_faq#QWidget_::activateWindow.28.29_-_behavior_under_windows)

哭了丶谁疼 2024-11-16 03:59:31

我是这样做的:

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

假设“this”是您的QMainWindow
工作起来就像一个魅力。

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.

九公里浅绿 2024-11-16 03:59:31
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
}

请注意,这还会打开 OSX 和 Windows 上其他虚拟桌面的窗口。我没有在 Linux 上测试过这个,但它可能可以工作。

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.

_失温 2024-11-16 03:59:31

这个问题不是 Windows 特有的……我在 Linux 上也有同样的问题。我的解决方案是在重新打开()窗口之前关闭()窗口。

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.

怀里藏娇 2024-11-16 03:59:31

对于 Windows,我使用 WinAPI 完成。您还需要知道窗口标题;

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

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

For Windows I did it with 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);
    }
}
 
蓝戈者 2024-11-16 03:59:31

以下内容借自论坛 它对我有用:

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文