其他分享
首页 > 其他分享> > Qt 鼠标穿透事件

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)

标签:transMouseEvents,Qt,void,QMouseEvent,穿透,event,鼠标
来源: https://www.cnblogs.com/lazy-ocelot-blog/p/16527829.html