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!