PDA

View Full Version : mapToScene, mapToItem doesn't work for child item after first draw (Qt 4.7)



Gourmet
26th February 2015, 17:42
In my main project I met problem. I created scene with items and they had subitems. I need map subitems points to scene and parent items coordinates. But this didn't work after first draw. After research I found inside Qt - the value of QTransform::m_dirty property remains 0 after child item was bond to parent item and all drawn. In this case mapToItem( parentItem(), QPointF(...) ) returned same QPointF(...) as parameter given. Almost same happened after call to mapToScene( QPointF(...) ) - it returned not scene coordinates but local QPointF(...) instead. That was most strange - after some user actions around scene f.e. drag-n-drop to scene, or opening modal window - all began work properly. But I was unhappy trying simulate this from code.

To work around this problem I created simple project. It's structure is very similar to my main project. It creates scene, item on it and adds subitem to item. Entire way is the same as I have in main project. And this test project gives same result as my main project - Subitem::mapToItem() does not work after draw. Here I attach this project. Anybody can feel free to test it in different Qt versions. In my Qt 4.7 installation I always get following result in console: Must be QPointF(30, 10), but got QPointF(10, 5)

If anybody can tell what I'm doing wrong or how to solve this problem - please tell me. This was greatly slowed my main project and I don't see the solution. PLEASE HELP!

wysota
26th February 2015, 18:50
The code behaves correctly (pos() of subitem is (0,0) and the item is not scaled so its coordinate system is identical to that of its parent , apart from the call to setPos() from within paint() which is of course invalid and causes things to work differently after the item is painted compared to before the item is painted. You also don't need to call prepareGeometryChange() in item constructor.

Gourmet
26th February 2015, 20:12
Damn, yes... You are absolutely right about setPos(). If it is placed in constructor - this works right. Thanks a lot.

I placed it to painter cause it did not properly calculated position if placed to constructor in main project. Changed main project, all works fine.