问题描述
开发环境:windows + QT
需求: 单击托盘将桌面窗口在被遮挡的情况下置顶
解决方案
方案1
-
资料链接
-
代码实现
Qt::WindowFlags flags = windowFlags(); this->setWindowFlags((flags | Qt::WindowStaysOnTopHint)); this->showMaximized(); this->setWindowFlags(flags); this->showMaximized();
-
验证结果
可实现遮挡窗口的情况置顶但有两个问题- setWindowFlags((flags | Qt::WindowStaysOnTopHint));后showMaximized、showMinimized失效
- 无法判断已置顶的情况,已置顶后重复调用会闪烁
方案2
- 资料链接
- 代码实现
this->activateWindow(); this->setWindowState((this->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive); this->raise(); #ifdef Q_OS_WIN32 HWND hForgroundWnd = ::GetForegroundWindow(); DWORD dwForeID = ::GetWindowThreadProcessId(hForgroundWnd, NULL); DWORD dwCurID = ::GetCurrentThreadId();::AttachThreadInput(dwCurID, dwForeID, TRUE); ::SetForegroundWindow((HWND)this->winId()); ::AttachThreadInput(dwCurID, dwForeID, FALSE); #endif this->setVisible(true);
- 验证结果
无法置顶,只在任务栏闪烁
方案3
- 代码实现
bool isMax = this->isMaximized(); if (!this->isMinimized()) {this->showMinimized(); }if (isMax) {this->showMaximized(); } else {this->showNormal(); }
- 验证结果
符合需求