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:
tWindowFlags(windowFla月球空心
gs() | Qt::WindowStaysOn宝贝英文怎么写
TopHint);
::SetWindowPos((HWND)winId(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
Linux:
tWindowFlags(windowFlags() | Qt::BypassWindowManagerHint);
或:
tWindowFlags(windowFlags() | Qt::X11BypassWindowManagerHint);
⽅式⼆:将创建最⼩化窗⼝,后将窗⼝显⽰
activateWindow();
tWindowState((windowState(在线电脑配置检测
) & ~Qt::WindowMinimized) | Qt::WindowActive);
rai();//必须加,不然X11会不起作⽤
#ifdef Q_OS_WIN32 //windows必须加这个,不然windows10 会不起作⽤,具体参看activateWindow 函数的⽂档
HWND hForgroundWnd = GetForegro高山仰止的意思
undWindow();
DWORD dwForeID = ::GetWindowThreadProcessId(hForgroundWnd, NULL); DWORD dwCurID = ::GetCurren百日誓师誓词
tThreadId();
::AttachThreadInput(dwCurID, dwForeID, TRUE);红烧鸡腿的做法
::SetForegroundWindow((HWND)winId());
::AttachThreadInput(dwCurID, dwForeID, FALSE);
#endif // MAC_OS
⽅式三:在QML中,在必要时再设置Window的风格
//激活窗⼝
topWindow.rai();
//置顶窗⼝
topWindow.flags |= Qt.WindowStaysOnTopHint
topWindow.show()
topWindow.flags &= ~Qt.WindowStaysOnTopHint