Results 1 to 4 of 4

Thread: How to efficiently get position of a QGraphicsItem in view coordinates?

  1. #1
    Join Date
    Jan 2012
    Posts
    66
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Platforms
    Windows

    Default How to efficiently get position of a QGraphicsItem in view coordinates?

    I'm trying to get the position in view coordinates of QGraphicsItems. I know that I can call QGraphicsItem:os() followed by QGraphicsView::mapFromScene(). This seems inefficient, though, because the view coordinates had to be calculated in order to draw the item on the screen, so mapping the item from scene to view just to get the coordinates seems like double the work. Is there a more direct way?

  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: How to efficiently get position of a QGraphicsItem in view coordinates?

    No, view coordinates don't have to be calculated as the transformation is inherited from the parent item. So this is the most direct way.
    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. The following user says thank you to wysota for this useful post:

    wayfaerer (8th February 2012)

  4. #3
    Join Date
    Jan 2012
    Posts
    66
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Platforms
    Windows

    Default Re: How to efficiently get position of a QGraphicsItem in view coordinates?

    Oh ok. Just so I understand you correctly, QGraphicsView::mapFromScene just returns a coordinate, it doesn't actually have to "map" it first? I had a look at the source and it looks like it is being mapped, though I could be wrong:

    Qt Code:
    1. QPoint QGraphicsView::mapFromScene(const QPointF &point) const
    2. {
    3. Q_D(const QGraphicsView);
    4. QPointF p = d->identityMatrix ? point : d->matrix.map(point);
    5. p.rx() -= d->horizontalScroll();
    6. p.ry() -= d->verticalScroll();
    7. return p.toPoint();
    8. }
    To copy to clipboard, switch view to plain text mode 

    EDIT: I realized I misunderstood you. So it does map it, and that's the most direct way. Bummer. Well, thanks for letting me know.
    Last edited by wayfaerer; 8th February 2012 at 01:54.

  5. #4
    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: How to efficiently get position of a QGraphicsItem in view coordinates?

    Sure it has to map it. And using mapToScene() will only work if your item doesn't have a parent item.

    A general approach is to use the following sequence:

    Qt Code:
    1. QRectF itemBR = item->boundingRect();
    2. QRectF sceneBR = item->mapToScene(itemBR);
    3. QRectF viewBR = view->mapFromScene(sceneBR);// viewBR is your rect/point/whatever
    To copy to clipboard, switch view to plain text mode 
    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.


Similar Threads

  1. QGraphicsItem coordinates, again
    By d_stranz in forum Qt Programming
    Replies: 15
    Last Post: 20th June 2011, 21:10
  2. Replies: 7
    Last Post: 21st March 2010, 03:11
  3. Replies: 0
    Last Post: 24th November 2008, 08:52
  4. Working with coordinates (QGraphicsScene/view)
    By maverick_pol in forum Qt Programming
    Replies: 5
    Last Post: 27th March 2008, 08:35
  5. Replies: 1
    Last Post: 26th September 2006, 05:38

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.