Results 1 to 6 of 6

Thread: How to display mouse coordinates in QGraphicsView

  1. #1
    Join Date
    May 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to display mouse coordinates in QGraphicsView

    Hello,

    How can I display the mouse coordinate position (XY) on my derived QGraphicsView class.

    I get the coordinates on mouseMoveEvent and even I can make a string with them, but I do not how to display the string and get it changed when the mouse moves.

    Thanks
    ejar

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to display mouse coordinates in QGraphicsView

    QGraphicsView is used to view the items which are in a QGraphicsScene. If you need to see the cursor position updating in the view, you need to something like this.

    Add a QGraphicsTextItem in your custom QGraphicsScene, and then update the the text of the item in QGraphicsScene::mouseMoveEvent()

    Qt Code:
    1. MyGraphicsScene::MyGraphicsScene(QObject* parent)
    2. : QGraphicsScene(parent)
    3. {
    4. cursor = new QGraphicsTextItem("0, 0", 0, this); //Fixed at 0, 0
    5. }
    6.  
    7. void MyGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
    8. {
    9. QString string = QString("%1, %2")
    10. .arg(event->scenePos().x())
    11. .arg(event->scenePos().y()); //Update the cursor potion text
    12.  
    13. cursor->setPlainText(string);
    14. }
    To copy to clipboard, switch view to plain text mode 

    I am not sure, if this what you are trying to do, looks like your are creating a custom QGraphicsView.

  3. #3
    Join Date
    May 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to display mouse coordinates in QGraphicsView

    Hi Santosh and thanks for your fast response.

    Yes, I think I am creating a custom QGraphicsView. I have got an intermediate solution to my question with the code you have sent, but the ideal would be to draw the text directly on my derived class from QGraphicsView.
    Let´s say that I want to display the mouse XY position always on the right bottom corner of the QGraphicsView. Doing on that way, it doesn´t matter about any scroll or zooming factor. Otherway I have to recalculate the position where to display the coordinates for each change on the view.

    My application is a GIS one, where I need to know the Latitude and Longitude of the cursor on the scene and display it at the right bottom corner of the view.

    Thanks
    ejar

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to display mouse coordinates in QGraphicsView

    As far as I understand you can only add items to a scene not to view, so you need to get the current view's view port and then add the cursor there, this needs to be in your scene, something like this

    Qt Code:
    1. void MyGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
    2. {
    3. // Get the list of the views attached to this scene
    4. QList<QGraphicsView*> scene_views = views();
    5.  
    6. for(int i = 0; i < scene_views.count(); i++)
    7. {
    8. // get the visible view port
    9. QWidget* viewport_widget = scene_views.at(i)->viewport();
    10.  
    11. // ensure it is visible
    12. if(viewport_widget->isVisible())
    13. {
    14. // Update cursor position (text to be displayed)
    15. QString string = QString("%1, %2")
    16. .arg(event->scenePos().x())
    17. .arg(event->scenePos().y());
    18.  
    19. cursor->setPlainText(string);
    20.  
    21. // Set the new position of the cursor
    22. cursor->setPos(viewport_widget->width() - cursor->boundingRect().width()
    23. ,viewport_widget->height() - cursor->boundingRect().height());
    24. }
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    May 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to display mouse coordinates in QGraphicsView

    Thanks again. The code above has the limitation of working with QGraphicsScene. Scene is fine becasue I have some graphics to display and it provides a very useful way, but it doesn´t for my mouse coordinates.
    Going back to the coordinate, is there a way to use something like QPainter::drawText and use it on the view. Is should be able to call the function each time the mouse moves.

    Ejar

  6. #6
    Join Date
    Feb 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to display mouse coordinates in QGraphicsView

    Hello I realize this is a old thread. Very interesting, I have questions on the cord system also...i would like to try this code but dont know quite how to do it. If you are still a member could you elaborate more?

Similar Threads

  1. tracking mouse coordinates
    By lightning2911 in forum Newbie
    Replies: 8
    Last Post: 11th December 2011, 23:51
  2. display image and get coordinates
    By ready in forum Newbie
    Replies: 9
    Last Post: 21st March 2011, 12:46
  3. Problem with QGraphicsView/Scene coordinates
    By Andrewshkovskii in forum Newbie
    Replies: 12
    Last Post: 19th October 2009, 10:41
  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

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.