Important: Please read the Qt Code of Conduct -

Solved:How to implement lastScenePos()?

  • Hi...
    according to Qt document:

    QPointF QGraphicsSceneMouseEvent::lastScenePos () const

    Returns the last recorded mouse cursor position in scene coordinates. The last recorded position is the position of the previous mouse event received by the view that created the event.

    I am not understand how to write it in application. Could you give me an example/suggestion how to write or implement this?

    Thank you.

  • Could you explain your case? This event is generated by the QGraphicsView when it receives mouse events. You just have to use this method, you don't need to implement it: it's already done in Qt.

  • Yes, I mean how to use it because I try like this but the coordinate not detected, always zero. I use buttonDownScenePos

    QPointF DialogApplication::get_koordinat_mouse_klik(){

    QPointF mouse_koordinat;
    QEvent::Type type;
    QGraphicsSceneMouseEvent mouseEvent(type);
    //mouse_koordinat = mouseEvent.lastScenePos();
    mouse_koordinat=   mouseEvent.buttonDownScenePos(Qt::LeftButton);
    return mouse_koordinat;


  • Well lastScenePos will only be provided if you are tracking the mouse: if it was clicked and dragged, or if you grabbed it. It will not be on the initial click.

  • I think it's better to use QGraphicsSceneMouseEvent::lastScenePos() inside one of those methods of the QGraphicsScene class :

    virtual void mouseDoubleClickEvent ( QGraphicsSceneMouseEvent * mouseEvent )
    virtual void mouseMoveEvent ( QGraphicsSceneMouseEvent * mouseEvent )
    virtual void mousePressEvent ( QGraphicsSceneMouseEvent * mouseEvent )
    virtual void mouseReleaseEvent ( QGraphicsSceneMouseEvent * mouseEvent )

    so you have to reimplement them !

  • Thank you very much issam and rcari. It's done well:
    void MyItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
    myMouse_koordinat = event->buttonDownScenePos
    qDebug() << myMouse_koordinat.x();

Log in to reply