我如何切换'总是在顶'一QMainWindow在脱,而不会导致闪烁或闪光吗?
-
27-09-2019 - |
题
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没有,"闪光"副作用?
解决方案
说没有:
这是不可能的,一旦窗口已不会产生闪烁创建更改窗口标志。闪烁是不可避免的,因为重新创建窗口的需要。
但有时,如果你坚持一个闪烁的效果有点难看这样,你可以故意将其拖出,使之看起来像什么“酷”就这样发生了。
也许弹出一个小的进度条,这不是在窗口,说“调整窗口属性!” ......淡出窗口不存在了,然后回来,并关闭进度条弹出。
其他提示
那么,对于一个解决方案,我想我会看在单声道音源,因为我知道.NET Form类(System.Windows.Forms的)有一个TopMost属性。
我发现我的Qt程序的解决方案是:
void MainWindow::on_actionAlways_on_Top_triggered(bool checked)
{
#ifdef Q_OS_WIN
// #include <windows.h>
if (checked)
{
SetWindowPos(this->winId(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
}
else
{
SetWindowPos(this->winId(), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
}
#else
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();
}
#endif
}
测试了
- 脱5.2.1在windows XP
- 脱5.2关OS X10.9
void ConsoleUI::onAllwaysTop(bool checked) { Qt::WindowFlags flags = windowFlags(); if (checked) { flags ^= Qt::WindowStaysOnBottomHint; flags |= Qt::WindowStaysOnTopHint; } else { flags ^= Qt::WindowStaysOnTopHint; flags |= Qt::WindowStaysOnBottomHint; } setWindowFlags(flags); show(); }
不隶属于 StackOverflow