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

Mapping mouse coordinates relative to a QGraphicsItem



  • On mouse press, my custom QGraphicsView calls the mouseInside(mousePosition) function of my QGraphicsItem. The mouseInside function looks like this:

    bool MyItem::mouseInside(QPoint mousePosition)
    {
        return this->rect().contains(mousePosition);
    }
    

    It is supposed to answer the question "Is the mouse inside the item?" except it doesn't work. The position of the item is (0, 0) in scene coordinates but the mousePosition QPoint is relative to the scene's top left corner. This means that if the item is in the middle of my scene and I click in the top left corner of the scene (where the item is NOT) it will say that the item was clicked. How can I map the item's position relative to the top left corner of the scene so that the function works?



  • I found the problem. I should have used QGraphicsScene::mousePressedEvent instead of QGraphicsView::mousePressedEvent.



  • I found the problem. I should have used QGraphicsScene::mousePressedEvent instead of QGraphicsView::mousePressedEvent.


Log in to reply