PDA

View Full Version : QGraphicsItem::pos () different from mapToParent(0, 0)



jado
18th January 2012, 10:57
Hi, I have a problem with QGraphicsItem::pos (), or just I don't get it.

From the doc of QGraphicsItem::pos (): "[...] this function returns the same as mapToParent(0, 0) [...]
That is true except when applying a transformation to the item that is not centered on it's origin; e.g.:

QTransform t;
t.translate(c.x(), c.y());
t.rotate(a);
t.translate(-c.x(), -c.y());
item->setTransform(t,true);

where a is an angle and c (in item's local coordinates) is a point different from (0,0).
For example:

transform(): QTransform(type=TxShear, 11=0.983923 12=0.178594 13=0 21=-0.178594 22=0.983923 23=0 31=13.7529 32=-52.7745 33=1)

pos(): QPointF(33, -246)
mapToParent(0,0): QPointF(46.7529, -298.774)
mapFromParent(pos()) : QPointF(-4.10658, 54.3822)


when I expect:

pos() == mapToParent(0,0)
(0,0) == mapFromParent(pos())


Any suggestions?
Thanks

jado
19th January 2012, 18:18
Hi,

the problem seems to be related to the translation, nobody has encountered it?

I believe I making something wrong, but also having QGraphicsItem::pos() != QGraphicsItem::mapToParent(0,0) seems quite strange to me, also because QGraphicsItem::scenePos() gives the correct value.

I had a look at the source code and I see that pos() is maintained in a member d_ptr->pos, while scenePos() is defined as mapToScene(0, 0); so is that possible that d_ptr->pos is not updated when setting a translation()?

Am I wrong?

Thanks

Hi again,
this is an example (from examples\graphicsview\diagramscene):

item = new DiagramItem(...);
item->setPos(0,0);
item->setTransform(QTransform().translate(50,100));

output:

item->pos(): QPointF(0, 0)
item->mapFromParent(item->pos()): QPointF(-50, -100)
item->mapToParent(0,0): QPointF(50, 100)
item->mapFromParent(item->mapToParent(0,0)): QPointF(0, 0)

item->scenePos(): QPointF(50, 100)
item->mapFromScene(item->scenePos()): QPointF(0, 0)
item->mapToScene(0,0): QPointF(50, 100)
item->mapFromScene(item->mapToScene(0,0)): QPointF(0, 0)

item->transform(): QTransform(type=TxTranslate, 11=1 12=0 13=0 21=0 22=1 23=0 31=50 32=100 33=1)

Thanks

jado
20th January 2012, 00:54
Hi,

simply setTransform(.) doesn't affect pos() ?

Thanks