Results 1 to 5 of 5

Thread: Coordinate problem

  1. #1
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Coordinate problem

    Hello!

    I have some problems with coordinates. I have a QGraphicsScene in a QGraphicsView and on the scene there is a QGraphicsItem. I retrieve the center coordinates of the bounding box
    Qt Code:
    1. QPointF coords= this->mapToScene(this->boundingRect().center());
    To copy to clipboard, switch view to plain text mode 
    How can I match the coordinate system for the expression above to one of those for mouse click coordinates?:
    Qt Code:
    1. void MyClass::mousePressEvent(QGraphicsSceneMouseEvent *event)
    2. {
    3. QPoint clickCoords = QCursor::pos();
    4. QPointF globalPos = event->screenPos();
    5. ...
    To copy to clipboard, switch view to plain text mode 

    I'm sorry, this coordinate thing has been discussed so many times, but I can't figure it out.

    Andy

  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: Coordinate problem

    Qt Code:
    1. event->scenePos();
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Coordinate problem

    Thank you very much, you opened my eyes. Now I realise, that I have focused too much on solving it from the other side, I was looking for a way to get the item boundingbox center coordinates to translate to global coordinates(btw how would I do that?, I didn't find a matching map to global function). But of course, for my needs there is no difference if global or scene corrdinates, they just have to match! Thank you!

  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: Coordinate problem

    QGraphcisScene coordinates connot be directly mapped to screen coordinates, as the on screen position of QGraphicsScene is taken care by QGraphicsView. So to get the screen coridinates of a point you have convert the point to QGraphicsSecen coordinates and then to screen codinates using the QGraphicsView which is displaying the QGraphicsScene.

    Qt Code:
    1. extern QGraphicsView * view;
    2. extern QGraphicsScene * scene;
    3. extern QGraphicsItem * item;
    4.  
    5. view->setScene(scene);
    6. scene->addItem(item);
    7.  
    8. QPointF point;
    9. point = item->boundingRect().center();
    10. point = item->mapToScene(point);
    11. point = view->mapFromScene(point);
    To copy to clipboard, switch view to plain text mode 

    Note that without having a view there is no sense for screen/viewport/global coordinates.
    Last edited by Santosh Reddy; 8th November 2012 at 07:48.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. The following user says thank you to Santosh Reddy for this useful post:

    AndyQT (8th November 2012)

  6. #5
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Coordinate problem - more an understanding problem [SOLVED]

    Thank you very much for the detailed explanation. It seems so logical when you see it.
    And of course, in my case I should also stick to the scene coordinates, so I don't need to care about the window position. In fact, all my problem started by choosing the wrong source for cursor position - QCursor:os() instead of the cursor scenePos supplied by the mouse event. I simply took what google gave me first :-(.

    Now everything is OK!

Similar Threads

  1. Get address from GPS coordinate
    By seanasl in forum Qt Programming
    Replies: 3
    Last Post: 24th January 2012, 07:09
  2. Problem with window-viewport coordinate
    By damon_1990 in forum Newbie
    Replies: 6
    Last Post: 15th September 2011, 12:17
  3. coordinate system
    By Wojtek.wk in forum Newbie
    Replies: 7
    Last Post: 12th April 2010, 13:47
  4. QGraphicsView Framework coordinate mapping problem
    By zgulser in forum Qt Programming
    Replies: 1
    Last Post: 14th September 2009, 12:30
  5. coordinate problem
    By sai in forum Qt Programming
    Replies: 1
    Last Post: 17th April 2009, 10:21

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.