QT 关于mousePressEvent无法过滤
bool Filter::eventFilter(QObject *watched, QEvent *event)
{// 判断是不是点击事件if((event->type() == QEvent::MouseButtonPress) || (event->type() == QEvent::MouseButtonDblClick)){//打印一个全局变量static int globalVar = 0;globalVar++;qDebug() << "global var:" << globalVar;qDebug() << "eventFilter";return true;}return QObject::eventFilter(watched, event);}void myQPushButton::mousePressEvent(QMouseEvent *e)
{//强转换为event类型//打印事件类型qDebug() << e->type();//自定义按键按qDebug() << "myQPushButton mousePressEvent";QPushButton::mousePressEvent(e);}
主要原因是双击事件他也定义为了mousePressEvent。所以拦截的时候QEvent::MouseButtonDblClick 也需要拦截下来不然会漏掉双击事件触发mousePressEvent导致过滤器不起作用。