[SOLVED] QGraphicsScene::itemAt() not working properly


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;
        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()));
              // some logic
          return true;

    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?


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.

Answered By – Pavan Chandaka

Answer Checked By – Jay B. (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *