QT窗口激活与置顶
背景
需要在屏幕在居中位置显示一个对话框,由用户来进行决策;且此对话框是非模态对话框。

实现方式
1、顶层窗口是一个Window,此窗口设置屏幕居中,透明。
2、对话框设计为Dialog,再将此Dialog挂载在Window上。
这样,只要Windows可能居中、置顶即可。
结果发现
此对话框并不会置顶显示,会被其他窗口挡住。
顶层窗口
Window { id:topWindow property int __hintdlgWidth: 480 property int __hintdlgHeight: 320 visible: true x: (Screen.width - __hintdlgWidth) / 2 y: (Screen.height - __hintdlgHeight) / 2 width: __hintdlgWidth+20 height: __hintdlgHeight+20 color: "#00000000" flags: Qt.FramelessWindowHint | Qt.Window | Qt.WindowStaysOnTopHint} |
Qt.WindowStaysOnTopHint
按照官方说法,该属性仅将最小化的窗口,再次置顶显示;而不是将一个新创建的窗口直接置顶显示。
解决方案
方式一:在C++中重置窗口位置
windows:setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);::SetWindowPos((HWND)winId(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);Linux:setWindowFlags(windowFlags() | Qt::BypassWindowManagerHint);或:setWindowFlags(windowFlags() | Qt::X11BypassWindowManagerHint); |
方式二:将创建最小化窗口,后将窗口显示
activateWindow(); setWindowState((windowState() & ~Qt::WindowMinimized) | Qt::WindowActive); raise();//必须加,不然X11会不起作用#ifdef Q_OS_WIN32 //windows必须加这个,不然windows10 会不起作用,具体参看activateWindow 函数的文档 HWND hForgroundWnd = GetForegroundWindow(); DWORD dwForeID = ::GetWindowThreadProcessId(hForgroundWnd, NULL); DWORD dwCurID = ::GetCurrentThreadId(); ::AttachThreadInput(dwCurID, dwForeID, TRUE); ::SetForegroundWindow((HWND)winId()); ::AttachThreadInput(dwCurID, dwForeID, FALSE);#endif // MAC_OS |
方式三:在QML中,在必要时再设置Window的风格
//激活窗口topWindow.requestActivate();topWindow.raise();//置顶窗口topWindow.flags |= Qt.WindowStaysOnTopHinttopWindow.show()topWindow.flags &= ~Qt.WindowStaysOnTopHint |

浙公网安备 33010602011771号