Qt 检测鼠标单击 titleBar (Windows) [英] Qt detecting mouse click on titleBar (Windows)

查看:26
本文介绍了Qt 检测鼠标单击 titleBar (Windows)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

event 或 mousePressEvent 函数适用于小部件内部,但我想在单击 titleBar(menuBar 的上部,包含关闭按钮等)时捕捉

event or mousePressEvent functions works for inside of a widget but I want to catch when clicked on a titleBar (upper part of menuBar, contains close button etc.)

我该怎么做?

推荐答案

您可以覆盖 nativeEvent,然后获取鼠标位置与几何(排除窗口框架)和 frameGeometry(包括窗口框架)进行比较,以检测是否碰到标题栏或不是

You can override nativeEvent, then get mouse position to compare with geometry (exclude window frame) and frameGeometry (include window frame) to detect if it hits title bar or not

bool MyClass::nativeEvent(const QByteArray & eventType, void * message, long * result)
{
    MSG* msg = (MSG*)(message);
    if (msg->message == WM_NCLBUTTONDOWN)
    {

        int mouseX = GET_X_LPARAM(msg->lParam);
        int mouseY = GET_Y_LPARAM(msg->lParam);
        QRect frame = frameGeometry();
        QRect content = geometry();

        qDebug() << "mouseX: " << mouseX << "mouseY:" << mouseY;
        qDebug() << "frame: " << frame;
        qDebug() << "content: " << content;

        if (mouseY < content.y() && mouseY >= frame.y())
        {
            qDebug() << "Hit title bar";
        }
    }

    *result = 0;
    return false;
}

这篇关于Qt 检测鼠标单击 titleBar (Windows)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆