1

I am trying to find out the QGraphicsItem under the mouse (when mouse hover over the item). For that I am using itemAt() method but itemAt() is not returning QGraphicsItem.
I tried this way:

bool myViewer::eventFilter(QObject *watched, QEvent *event)
{
    bool fEvent = false;
    switch(event->type())
    {
        case QEvent::MouseButtonPress:
        {....}
        case QEvent::MouseButtonRelease:
        {...}
        case QEvent::Enter:
        {
           QMouseEvent * mouseEvent = static_cast<QMouseEvent *>(event);
           qDebug()<<"Event points = "<< mouseEvent->pos(); // showing points properly
           QPointF po = _view->mapToScene(mouseEvent->pos());       
           
           QGraphicsRectItem* rItem = qgraphicsitem_cast<QGraphicsRectItem*>(_scene->itemAt(po,QTransform()));
           if(rItem)
           {
              // some logic
           }
        }
          return true;

       default:
           break;
    }
    return fEvent;

         

I tried with QGraphicsView::itemAt() also. But still it did not work.
Note: In both way following lines works fine.

 QMouseEvent * mouseEvent = static_cast<QMouseEvent *>(event);
 qDebug()<<"Event points = "<< mouseEvent->pos();     
 

Where am I wrong?

tushar
  • 313
  • 4
  • 10
  • Looks like you are using `static_cast` to downcast a `QEvent` to `QMouseEvent`, but at least in the `QEvent::Enter` case, `event` will be pointing to a `QEnterEvent` object, which is not a subclass of `QMouseEvent`, and therefore that `static_cast` will invoke undefined behavior. – Jeremy Friesner Mar 23 '22 at 19:17
  • @JeremyFriesner So how should I proceed ? Can you help me ? – tushar Mar 23 '22 at 19:20
  • I think you should probably delete the `case QEvent::Enter:` line since a `QEnterEvent` doesn't have any well-defined position, and therefore it doesn't make sense to query the QGraphicsView about a position that you haven't got. – Jeremy Friesner Mar 23 '22 at 19:59
  • @JeremyFriesner I debugged and found that, `mouseEvent->pos()` is giving wrong points. That's why `itemAt()` is not selecting any `QGraphicsItem`. Manually I gave correct point to `itemAt()` and I got correct output. Why `mouseEvent->pos()` is not giving correct point ? – tushar Mar 24 '22 at 05:14
  • `mouseEvent()->pos()` gives a position in pixels relative to the top-left corner of the `QGraphicsView`. You'll need to translate that position into the coordinate-system of the `QGraphicsScene` (via `QGraphicsView::mapToScene(QPoint)`) in order to obtain co-ordinate values that are appropriate to pass to the QGraphicsScene's `itemAt()` method. – Jeremy Friesner Mar 24 '22 at 05:25
  • @JeremyFriesner I did that but still not working. The point is showing `QPointF(449,1562)` but actual point is `QPoint(295,77)` – tushar Mar 24 '22 at 05:36

1 Answers1

1

May be you can try handling QEvent::GraphicsSceneMousePress

And then get the position as said below (pseudo code. Not tetsted.)

//Get the scene event object.
QGraphicsSceneMouseEvent* sceneEvent = dynamic_cast<QGraphicsSceneMouseEvent*>(qevent);

//From the scene event object get the scene position.
QPointF po = sceneEvent->scenePos();

Use that position to find the item.

Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34
  • Sorry but, its not working. – tushar Mar 24 '22 at 02:16
  • Just a random try, can you just try with other position functions like QGraphicsSceneMouseEvent::pos() or QGraphicsSceneMouseEvent::screenPos() or QGraphicsSceneMouseEvent::lastScreenPos() or sceneEvent->buttonDownScenePos(Qt::LeftButton) – Pavan Chandaka Mar 24 '22 at 02:29