Qt应用程序,最小化到托盘,在Win7上存在焦点窃取预防问题
我有一个 Qt 应用程序,它与 Skype 一样,通常最小化到托盘中。当用户单击托盘图标时,将显示应用程序窗口。
这在 Linux 和 WinXP 上运行良好。然而,在 Win7 上,应用程序窗口会显示,但位于其他窗口下方 - 除非当前活动窗口是 Qt Creator,或者是我的应用程序(在我将其最小化到托盘之前)。所以这一定与预防焦点窃取有关。
我知道 Skype 是用 Qt 编写的,他们没有这个问题,所以它一定是可以修复的。
这是我的代码:
void MainWindow::toggleVisible(QSystemTrayIcon::ActivationReason reason) { if (QSystemTrayIcon::Trigger == reason) setVisible(!isVisible()); }
[编辑] 事实证明我必须调用 activateWindow。我将代码更改为:
void MainWindow::toggleVisible(QSystemTrayIcon::ActivationReason reason) { if (QSystemTrayIcon::Trigger == reason) { if (isVisible()) { hide(); } else { show(); raise(); activateWindow(); } } }
现在可以在 Win7 上运行。
I have a Qt app which, like Skype, is usually minimized to the tray. When the user clicks the tray icon, the app window is shown.
This works fine on Linux and WinXP. On Win7 however, the app window is shown but stays below the other windows - unless the currently active window is Qt Creator, or was my app (before I minimized it to tray). So it must have something to do with focus stealing prevention.
I know Skype is written in Qt, and they don't have that problem, so it must be fixable.
Here's my code:
void MainWindow::toggleVisible(QSystemTrayIcon::ActivationReason reason) { if (QSystemTrayIcon::Trigger == reason) setVisible(!isVisible()); }
[Edit]
It turns out I had to call activateWindow. I changed my code to:
void MainWindow::toggleVisible(QSystemTrayIcon::ActivationReason reason) { if (QSystemTrayIcon::Trigger == reason) { if (isVisible()) { hide(); } else { show(); raise(); activateWindow(); } } }
It works on Win7 now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用以下代码使我的应用程序在从托盘中单击时可见;
这将处理最小化的情况,然后您单击托盘图标。
I use the following code to make my application visible when clicked from tray;
this will handle the case its in minimized and you click the tray icon.