QT窗口激活与置顶

更新时间:2023-05-03 04:10:55 阅读: 评论:0

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

本文发布于:2023-05-03 04:10:55,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/519900.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:对话框   居中   屏幕   参看   设置   置顶   结果
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图