Jak*_*les 22 c++ qt window-managers
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();
}
}
Run Code Online (Sandbox Code Playgroud)
上面的解决方案有效,但因为setWindowFlags隐藏了窗口,所以需要重新显示它,当然看起来并不优雅.那么如何在没有"闪烁"副作用的情况下为QMainWindow切换"永远在线"?
Jak*_*les 16
好吧,对于一个解决方案,我认为我会查看Mono源代码,因为我知道.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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24169 次 |
| 最近记录: |