Qt 鼠标穿透事件

当前窗口及子控件均不响应鼠标事件

setAttribute(Qt::WA_TransparentForMouseEvents, true);

当前窗口透明区域不响应鼠标事件

setWindowFlags(Qt::FramelessWindowHint | Qt::Tool);
setAttribute(Qt::WA_TranslucentBackground, true);
自定义当前窗口区域响应鼠标事件(注意:如果设置的区域很复杂,效果可能会很慢。)
void QWidget::setMask(const QRegion &region)

自行传递当前窗口的鼠标事件

复制代码
void mouseMoveEvent(QMouseEvent *event)
{
    transMouseEvents(event);
}

void mousePressEvent(QMouseEvent *event)
{
    transMouseEvents(event);
}

void mouseReleaseEvent(QMouseEvent *event)
{
    transMouseEvents(event);
}

void mouseDoubleClickEvent(QMouseEvent *event)
{
    transMouseEvents(event);
}

void transMouseEvents(QMouseEvent *event)
{
    if (this->parentWidget()) {    // 这里仅演示向父类窗口传递鼠标事件
    this->setAttribute(Qt::WA_TransparentForMouseEvents, true);

    QPoint pt = this->mapTo(this->parentWidget(), event->pos());
    QWidget *wid = this->parentWidget()->childAt(pt);
    if (wid) {
        pt = wid->mapFrom(this->parentWidget(), pt);
        QMouseEvent *mEvent = new QMouseEvent(event->type(), pt, event->button(), event->buttons(), event->modifiers());
        QApplication::postEvent(wid, mEvent);
    }

    this->setAttribute(Qt::WA_TransparentForMouseEvents, false);
    }
}
复制代码

 

摘自:

  QT 鼠标穿透 - 知乎 (zhihu.com)

posted @   eric_zw  阅读(1047)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示