如何切换“始终位于顶部” Qt 中的 QMainWindow 不会引起闪烁或闪烁吗?
void MainWindow::on_actionAlways_on_Top_triggered(bool checked)
{
Qt::WindowFlags flags = this->windowFlags();
if (checked)
{
this->setWindowFlags(flags | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);
this->show();
}
else
{
this->setWindowFlags(flags ^ (Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint));
this->show();
}
}
上面的解决方案有效,但是因为 setWindowFlags 隐藏了窗口,所以需要重新显示它,当然这看起来不太优雅。那么如何为 QMainWindow 切换“始终在顶部”而不产生“闪烁”副作用呢?
void MainWindow::on_actionAlways_on_Top_triggered(bool checked)
{
Qt::WindowFlags flags = this->windowFlags();
if (checked)
{
this->setWindowFlags(flags | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);
this->show();
}
else
{
this->setWindowFlags(flags ^ (Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint));
this->show();
}
}
The above solution works but because setWindowFlags hides the window, it needs to be re-shown and of course that doesn't look very elegant. So how do I toggle "always on top" for a QMainWindow without that "flashing" side effect?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
诺基亚说不:
但有时,如果您遇到像这样丑陋的闪烁效果,您可以故意将其拖出,使其看起来像是刚刚发生的“很酷”的事情。
也许会弹出一个不在窗口中的小进度条,说“调整窗口属性!”...使窗口淡出,然后返回,然后关闭进度条弹出窗口。
Nokia says no:
But sometimes if you're stuck with a flashing effect that's kind of ugly like this, you can intentionally drag it out to make it seem like something "cool" just happened.
Maybe pop up a little progress bar that's not in the window, say "Adjusting Window Properties!"...fade the window out of existence and then back in, and close the progress bar popup.
好吧,对于解决方案,我认为我应该查看 Mono 源代码,因为我知道 .NET Form 类 (System.Windows.Forms) 有一个 TopMost 属性。
我为我的 Qt 程序找到的解决方案是:
Well, for a solution I figured I'd look in the Mono sources, since I know the .NET Form class (System.Windows.Forms) has a TopMost property.
The solution I found for my Qt program was:
因为我最近遇到了同样的问题:
你可以通过绕过 Qt 来做到这一点。对于 Windows 部分,请参阅 @JakePetroules 答案。
我使用的 XCB (X11) 版本:
使用方式如下:
xcb_update_prop(true, window()->winId(), "_NET_WM_STATE", "_NET_WM_STATE_ABOVE", "_NET_WM_STATE_STAYS_ON_TOP");
它有点 hackish,但在 Mate、KDE、GNOME3 上运行良好, XFCE 和 openbox。
Since i recently ran into the same issue:
You can do it by bypassing Qt. For the windows part see @JakePetroules answer.
XCB (X11) version i use:
Use like:
xcb_update_prop(true, window()->winId(), "_NET_WM_STATE", "_NET_WM_STATE_ABOVE", "_NET_WM_STATE_STAYS_ON_TOP");
Its a bit hackish, but it worked fine on Mate, KDE, GNOME3, XFCE and openbox.
使用 Qt 5.2.1 进行测试
Tested with