Results 1 to 3 of 3

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

  1. #1
    Join Date
    Apr 2011
    Posts
    31
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default mapToScene, mapToItem doesn't work for child item after first draw (Qt 4.7)

    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!
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: mapToScene, mapToItem doesn't work for child item after first draw (Qt 4.7)

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2011
    Posts
    31
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: mapToScene, mapToItem doesn't work for child item after first draw (Qt 4.7)

    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.
    Last edited by Gourmet; 26th February 2015 at 20:39.

Similar Threads

  1. MapToItem, MapToScene
    By mvbhavsar in forum Newbie
    Replies: 7
    Last Post: 22nd November 2017, 17:45
  2. Replies: 1
    Last Post: 13th November 2014, 08:15
  3. Using QGraphicsItem::mapToScene
    By Casper14 in forum Qt Programming
    Replies: 2
    Last Post: 22nd July 2014, 21:40
  4. Replies: 2
    Last Post: 2nd July 2012, 12:54
  5. remove item for QGirdLayout doesn't work.
    By klnusbaum in forum Qt Programming
    Replies: 4
    Last Post: 23rd May 2008, 23:04

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.