Results 1 to 3 of 3

Thread: Using QGraphicsItem::mapToScene

  1. #1
    Join Date
    Jun 2014
    Posts
    7
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Using QGraphicsItem::mapToScene

    Initially I thought this would be a trivial problem to solve, but I must be misundertanding something, because I just cannot get it working. I have subclassed QGraphicsItem to create my own custom QGraphicsItem, MyItem. This item is then added to the scene of a QGraphicsView.

    In MyItem, I have implemented MyItem::itemChange, and I want to get the position of the item, but in the coordinates of the QGraphicsView's scene. However, when I use MyItem::mapToScene, I always receive the coordinates of the item, just multiplied by two. Here is the relevant code:

    Qt Code:
    1. MyItem::
    2. itemChange(GraphicsItemChange change, const QVariant &value)
    3. {
    4. if (change == QGraphicsItem::ItemPositionChange)
    5. {
    6. double x = this->pos().x();
    7. double y = this->pos().y();
    8. QPointF scene = mapToScene(QPointF(x, y));
    9. assert (this->scene() != 0);
    10. std::cout << "item coordinates: " << x << " " << y << "\n";
    11. std::cout << "scene coordinates: " << scene.x() << " " << scene.y() << "\n";
    12. }
    13.  
    14. return QGraphicsItem::itemChange(change, value);
    15. }
    To copy to clipboard, switch view to plain text mode 

    The incorrect output is then:

    Qt Code:
    1. item coordinates: -27.2653 190.857
    2. scene coordinates: -54.5306 381.714
    To copy to clipboard, switch view to plain text mode 

    I would expect mapToScene to return the same coordinates as where the item was drawn when I called this->scene()->addItem(myitem); in my QGraphicsView, which then calls MyItem:aint.

    How can I know where in the scene the item is? Thanks!

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Using QGraphicsItem::mapToScene

    I want to get the position of the item, but in the coordinates of the QGraphicsView's scene
    Why do you need that ? Ideally an item should draw itself correctly without knowing where it is located.
    If you really need it, you can use QGraphicsItem::scenePos() method, which is equivalent to QGraphicsItem::mapToScene(0,0).
    btw. pos() will return position relative to item's parent, or scene:
    (Qt docs) QGraphicsItem:os
    Returns the position of the item in parent coordinates. If the item has no parent, its position is given in scene coordinates.
    So in your case you might be transforming the scene position via scene's transform again.

  3. #3
    Join Date
    Jun 2014
    Posts
    7
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Using QGraphicsItem::mapToScene

    Thanks for your reply. I need it to ensure that the user does not move the QGraphicsItem outside the valid bounds of the scene. So I need to know where the item is relative to the scene's coordinates to calculate whether its position is valid.

    In the end, I just added the item I want to draw at (0, 0), and then moved it myself using setPos(x, y). This way it lives in the same coordinate system as the scene and makes my life much easier. See http://osdir.com/ml/lib.qt.jambi/2008-05/msg00067.html

Similar Threads

  1. Replies: 7
    Last Post: 29th November 2010, 20:20
  2. Having problem with QGraphicsItem
    By Kingofhearts_sri in forum Qt Programming
    Replies: 4
    Last Post: 23rd January 2009, 20:10
  3. Casting QGraphicsItem child from QGraphicsItem
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 29th August 2008, 16:37
  4. Replies: 2
    Last Post: 28th June 2008, 17:31

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.