PDA

View Full Version : QGraphicsPixmapItem's boundingRect() pos() different from item's pos() when dragging?



keshav2010
30th July 2018, 02:00
Hello,
My application is based on the graphics view framework and so I'm trying to draw some images ( as loaded by user) and allow user to drag them around the scene
so basically I made a class "AnimatableItem" that inherits from QGraphicsPixmapItem
and this item has flag set to make it movable.

I've overrided AnimatableItem::paint() method as follow


//override
void AnimatableItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->setPen(QColor(200,100,50));
painter->drawPixmap(boundingRect().x(), boundingRect().y(), boundingRect().width(), boundingRect().height(), this->pixmap());

painter->setBrush(QBrush(QColor(0,0,0)));
painter->drawRect(this->pos().x(), this->pos().y(), this->boundingRect().width(), this->boundingRect().height());
}


as you see, im drawing a pixmap which apparently seems to be at desired position
however the 2nd rectangle (black in color) which represents item position ( actual ) is always ahead of boundingRect position and keeps getting farther away as i move more toward
+x axis and +y axis , how do i make sure item's position and boundingRect() position is same ?

d_stranz
30th July 2018, 17:56
You are dealing with two different coordinate systems here. From the documentation for "The Graphics View Coordinate System":


Items live in their own local coordinate system. Their coordinates are usually centered around its center point (0, 0), and this is also the center for all transformations. Geometric primitives in the item coordinate system are often referred to as item points, item lines, or item rectangles. ... an item's bounding rect and shape are in item coordinates.


An item's position is the coordinate of the item's center point in its parent's coordinate system; sometimes referred to as parent coordinates. The scene is in this sense regarded as all parent-less items' "parent". Top level items' position are in scene coordinates.

For pixmaps, the coordinates are in pixels.

Any drawing you do should be in item coordinates; the graphics view system will transform these coordinates as needed to translate, rotate, or scale an individual graphics item. The item itself does not need to be aware of how it is transformed when being painted.