欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 产业 > 被遮挡QT窗口置顶

被遮挡QT窗口置顶

2025/2/23 1:34:02 来源:https://blog.csdn.net/ZaiLuShang2121/article/details/145268266  浏览:    关键词:被遮挡QT窗口置顶

问题描述

开发环境:windows + QT
需求: 单击托盘将桌面窗口在被遮挡的情况下置顶

解决方案

方案1

  • 资料链接

  • 代码实现

    Qt::WindowFlags flags = windowFlags();
    this->setWindowFlags((flags | Qt::WindowStaysOnTopHint));
    this->showMaximized();
    this->setWindowFlags(flags);
    this->showMaximized();
    
  • 验证结果
    可实现遮挡窗口的情况置顶但有两个问题

    1. setWindowFlags((flags | Qt::WindowStaysOnTopHint));后showMaximized、showMinimized失效
    2. 无法判断已置顶的情况,已置顶后重复调用会闪烁

方案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();
    }
    
  • 验证结果
    符合需求

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词