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:
MyItem::
itemChange(GraphicsItemChange change,
const QVariant &value
) {
{
double x = this->pos().x();
double y = this->pos().y();
assert (this->scene() != 0);
std::cout << "item coordinates: " << x << " " << y << "\n";
std::cout << "scene coordinates: " << scene.x() << " " << scene.y() << "\n";
}
}
QVariant
MyItem::
itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change == QGraphicsItem::ItemPositionChange)
{
double x = this->pos().x();
double y = this->pos().y();
QPointF scene = mapToScene(QPointF(x, y));
assert (this->scene() != 0);
std::cout << "item coordinates: " << x << " " << y << "\n";
std::cout << "scene coordinates: " << scene.x() << " " << scene.y() << "\n";
}
return QGraphicsItem::itemChange(change, value);
}
To copy to clipboard, switch view to plain text mode
The incorrect output is then:
item coordinates: -27.2653 190.857
scene coordinates: -54.5306 381.714
item coordinates: -27.2653 190.857
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!
Bookmarks