zoukankan      html  css  js  c++  java
  • 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
         __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.WindowStaysOnTopHint
    topWindow.show()
    topWindow.flags &= ~Qt.WindowStaysOnTopHint
  • 相关阅读:
    深入Spring Security魔幻山谷-获取认证机制核心原理讲解
    灵魂拷问:你真的理解System.out.println()打印原理吗?
    解决分布式React前端在开发环境的跨域问题
    “我以为”与“实际上”
    前端:如何让background背景图片进行CSS自适应
    VSCode如何设置Vue前端的debug调试
    【JDBC】总结
    【JDBC第9章】Apache-DBUtils实现CRUD操作
    【JDBC第8章】数据库连接池
    【JDBC第7章】DAO及相关实现类
  • 原文地址:https://www.cnblogs.com/gd-luojialin/p/15027758.html
Copyright © 2011-2022 走看看