如何正确实现“最小化到托盘” Qt 中的函数?
如何在 Qt 中正确实现“最小化到托盘”功能?
我在 QMainWindow::changeEvent(QEvent *e)
中尝试了以下代码,但窗口只是最小化到任务栏,并且恢复时客户区域显示为空白。
if (Preferences::instance().minimizeToTray())
{
e->ignore();
this->setVisible(false);
}
试图忽略该事件似乎也没有任何作用。
How do I properly implement a "minimize to tray" function in Qt?
I tried the following code inside QMainWindow::changeEvent(QEvent *e)
, but the window simply minimizes to the taskbar and the client area appears blank white when restored.
if (Preferences::instance().minimizeToTray())
{
e->ignore();
this->setVisible(false);
}
Attempting to ignore the event doesn't seem to do anything, either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
显然,处理其他事件需要一点延迟(也许有人会发布确切的细节?)。这就是我最终所做的,效果非常好:
Apparently a small delay is needed to process other events (perhaps someone will post the exact details?). Here's what I ended up doing, which works perfectly:
除了 Jake Petroules 所说的之外,看来只要做:
就足够了。来自 http://qt-project.org/doc/qt-4.8 /qtimer.html#details :
这样你就不会遇到选择合适的延迟值的问题......
In addition to what Jake Petroules said, it appears that simply doing:
is enough. From http://qt-project.org/doc/qt-4.8/qtimer.html#details :
This way you don't have the problem of selecting an appropriate delay value...
这就是我实现“最小化到托盘”的方式。现在,您可以通过双击图标或右键单击并在菜单中选择“显示/隐藏”来最小化。
That's how I realize a "minimize to tray". You can now minimize either by double clicking on the icon, or by right-clicking and selecting "Show/Hide" in the menu.
我发现
showMinimized()
插槽可以在没有 QTimer 延迟的情况下工作,因此您可以在
main()
中使用如下代码来显示主窗口并立即将其图标化,当需要时。I have found that the
showMinimized()
slot works without a QTimer delay, so you can use code like:in your
main()
to show a main window and immediately iconize it, when desired.