Results 1 to 3 of 3

Thread: Getting mouse coordinates from the QGraphicsView's referential

  1. #1
    Join Date
    Jan 2013
    Posts
    15
    Qt products
    Qt5
    Platforms
    Windows

    Exclamation Getting mouse coordinates from the QGraphicsView's referential

    I'm using Qt 4.8.4 on Qt Creator 2.0.1.

    I have created a new widget to replace QGraphicsView on my mainwindow, in order to override wheelevents, mousepressevents, etc. I logically inherit QGraphicsView.

    I'm using a mousemoveevent to show the coordinates on a label, also on the widget.

    This is my code

    Qt Code:
    1. void Graph::mouseMoveEvent(QMouseEvent *event)
    2. {
    3. QPointF position = event->posF();
    4.  
    5. ui->mouse_coordinates->setText("( " + QString::number((int)position.x())
    6. + ", " + QString::number((int)position.y())
    7. + " )");
    8. }
    To copy to clipboard, switch view to plain text mode 

    But this always show the coordinates in the widget's window (being (0, 0) the top left corner). Because I use zoom and scroll bars on my reimplemented QGraphicsView, these absolute coordinates are useless, since I need this to identify points in a graph drawn in the View.

    Is there a way of converting these coordinates to the QGraphicsView referential, i.e., for example, if I have a square drawn at the QGraphicsView (0, 0) (that's not located at the top left corner), how do I get the coordinates (0, 0) when my mouse hovers that square?

    P.S.: I have tried this with mousepressevent as well with the same result

    Thank you,
    Bernardo M.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Getting mouse coordinates from the QGraphicsView's referential

    They already are in the view's coordinate space. What you want is the scene coordinate space. You can use QGraphicsView::mapToScene() for that or you can reimplement the mouse event directly in the scene.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2013
    Posts
    15
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Getting mouse coordinates from the QGraphicsView's referential

    Quote Originally Posted by wysota View Post
    They already are in the view's coordinate space. What you want is the scene coordinate space. You can use QGraphicsView::mapToScene() for that or you can reimplement the mouse event directly in the scene.
    Yes, I'm sorry, that's exactly what I meant.
    I wiil try that. thank you.
    Last edited by bshikari; 29th March 2013 at 04:21.

Similar Threads

  1. Replies: 5
    Last Post: 3rd March 2016, 15:42
  2. tracking mouse coordinates
    By lightning2911 in forum Newbie
    Replies: 8
    Last Post: 11th December 2011, 23:51
  3. how to get mouse coordinates from Qgraphicsview
    By ready in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2011, 05:33
  4. QGraphicsView coordinates transformation
    By mtribaldos in forum Newbie
    Replies: 1
    Last Post: 6th February 2008, 17:44
  5. getting mouse coordinates
    By eric in forum Qt Programming
    Replies: 1
    Last Post: 14th November 2007, 19:34

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.