Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

mouseEvent->pos().x() always return 0. mouseEvent->pos().y() updating on mouse move



  • mouseEvent->pos().x() always return 0. mouseEvent->pos().y() updating on mouse move

    
    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;
    }```

  • Lifetime Qt Champion

    Since you check for GraphicsSceneMouseMove I would guess it's a QGraphicsSceneMouseEvent, not a QMouseEvent.



  • updated code qInfo() working but QToolTip shows eroor error: no matching function for call to ‘QToolTip::showText(QPointF, const QString).

    bool Module3::eventFilter(QObject *obj, QEvent *event)
    {
     
    
        if (obj == scene && event->type() == QEvent::GraphicsSceneMouseMove) {
        
             if (auto *mouseEvent = dynamic_cast<QGraphicsSceneMouseEvent*>(event)) {
              qInfo()<<"x"<<mouseEvent->scenePos().x()<<"y"
                <<mouseEvent->scenePos().y();
    
                       QToolTip::showText(mouseEvent->scenePos(),
                            QString::number(
                            mouseEvent->scenePos().x()) +
                         ", "  + QString::number( 
                           mouseEvent->scenePos().y())); 
    
    
            }
         }
      }
    
    
    

  • Lifetime Qt Champion

    Because, as the compiler complains - QToolTip::showText() needs a QPoint instead a QPointF - so you have to convert it.



  • This code works

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

  • Lifetime Qt Champion

    @sujith-D Then please mark this topic as solved, thx


Log in to reply