Important: Please read the Qt Code of Conduct -

Retrieve mouse coordinates from QGraphicsView

  • Hello,
    So i ve been working on this problem for hours and hours . i have looked and tried a lot of things and it seems nothing is working.
    So basically i have a sub-class of QGraphicsView where a scene is created ( sceneAutomate ) .
    in the scene i can add rect and delete them ( i am working on langton ants , so the rects for me are the ants ). And i want to add an "ant" on the scene when i double click it which is easy but i can't add it on the spot where i have clicked twice, it s adding the "ants" on different place of where i have clicked twice.
    this is the code i am using :

    • to get position and add "ant" on position :
    void InterfaceAutomate::mouseDoubleClickEvent(QMouseEvent *mouseEvent)
        QPointF  pos1= mouseEvent->pos();
        int x = pos1.x();
        int y = pos1.y();

    so with this i have used qDebug to know where i clicked and compare it to where the "ant" is created and it is sometimes the same and other times different, but when the values are the same the "ant" is not created at the right spot, i don t really know what QMouseEvent do exactly since i have been using qt for almost three weeks so i am still new to it, i have tried QGraphicsSceneMouseEvent but it does not work and from what i ve read i cant use it when i inherit from QGraphicsView.

    i can share the whole code if you can give me the right website to upload it .
    thanks a lot

  • Qt Champions 2017

    It could be a an issue with View to Scene co-ordinate conversion. Just check it. Another option is you can try inheriting from scene, re-implement mouse handlers and use the co-ordinates.

  • Thanks you @dheerendra for the reply.
    How i can do that ( the check ) ?
    and i can't inherit another time from scene it will take me a long time to redo the job since i am using some functions that are particular to graphicsview

  • Solved finally, i had a problem on the class where i create the rect
    Thanks to all of you

Log in to reply