简体   繁体   English

QT eventFilter mouseEvent->pos().x() 始终为零但 mouseEvent->pos().y() 工作正常

[英]QT eventFilter mouseEvent->pos().x() always zero but mouseEvent->pos().y() working fine

mouseEvent->pos().x() always return 0. mouseEvent->pos().y() updating on mouse move mouseEvent->pos().x() 总是返回 0。 mouseEvent->pos().y() 在鼠标移动时更新

bool Module3::eventFilter(QObject *obj, QEvent *event) { 

      QMouseEvent *mouseEvent = static_cast(event);

      if(obj ==scene && event->type() == QEvent::GraphicsSceneMouseMove){

          QToolTip::showText(mouseEvent->pos(),QString::number(mouseEvent->pos().x()) + 
           ", "  + QString::number( mouseEvent->pos().y()));
       }

       return false;
}

You have undefined behaviour.你有未定义的行为。 If event->type() is QEvent::GraphicsSceneMouseMove then the real type of event is QGraphicsSceneMouseEvent* not QMouseEvent* .如果event->type()QEvent::GraphicsSceneMouseMove那么真正的事件类型是QGraphicsSceneMouseEvent*而不是QMouseEvent*

Try...尝试...

if (obj == scene && event->type() == QEvent::GraphicsSceneMouseMove) {
    if (auto *mouseEvent = dynamic_cast<QGraphicsSceneMouseEvent *>(event)) {
        QToolTip::showText(mouseEvent->scenePos().toPoint(),QString::number(mouseEvent->pos().x()) + 
            ", "  + QString::number( mouseEvent->pos().y()));
    }
}
return false;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM