Results 1 to 2 of 2

Thread: coordinate transformation of graphics item

  1. #1
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default coordinate transformation of graphics item

    Hello forum,

    There are three functions i confused to use, when to use, they are:

    1. mapFromItem(....)

    2. mapToItem(...)

    3. mapFromParent(....)

    4. mapFromScene(...)


    Please take a look at the attached image. Screenshot-3.jpg


    You can see that a small ellipse is positioned inside the large ellipse.


    While defining the bounding rectangle for each of them, it of its center point is defined to be at (0,0) as follows:


    Bounding rectangle for the large ellipse
    Qt Code:
    1. QRectF RootGraphicsItem::boundingRect() const
    2. {
    3.  
    4. //!get the bounding rectangle of the enclosing text
    5. QRectF rect = m_textGraphicsItem->boundingRect();
    6.  
    7. // qDebug() << "************** the bounding rectangle of the text graphics item: *************" << endl;
    8. // qDebug() << "Top left: " << rect.topLeft() << endl;
    9. // qDebug() << "Center: " << rect.center() << endl;
    10. // qDebug() << "Bottom right: " << rect.bottomRight() << endl;
    11.  
    12. //the following condition decides if the boundary needs
    13. //to be changed
    14. if( rect.width() < m_drawRectMinimumWidth )
    15. rect.setWidth(m_drawRectMinimumWidth);
    16.  
    17. if(rect.height() < m_drawRectMinimumHeight)
    18. rect.setHeight(m_drawRectMinimumHeight);
    19.  
    20. //set the rectangle width with the text item spacing
    21. rect.setWidth(rect.width() + getTextItemSpacing() );
    22.  
    23. //the bounding rectangle will have its center at (0,0)
    24. rect.translate(-rect.center());
    25.  
    26.  
    27. // qDebug() << "After the adjustments" << endl;
    28. // qDebug() << "Center: " << rect.center() << endl;
    29. // qDebug() << "Top Left: " << rect.topLeft() << endl;
    30. // qDebug() << "Bottom Right: " << rect.bottomRight() << endl;
    31.  
    32.  
    33.  
    34. //and return the rectangle
    35. return rect;
    36. }
    To copy to clipboard, switch view to plain text mode 


    and Bounding rectangle for the small ellipse

    Qt Code:
    1. QRectF H3DHierarchyArrowDockGraphicsItem::boundingRect() const
    2. {
    3. QRectF rect(0,0,sizeNormal.width(),sizeNormal.height());
    4.  
    5. //center will be (0,0)
    6. rect.translate(-rect.center());
    7.  
    8. return rect;
    9. }
    To copy to clipboard, switch view to plain text mode 


    Now when i layout the smaller one in the function of large one's function definition, which of the mapping function i should be using ?


    Right now i am doing it as follows, seems like a hard-coding to me, What you guys think ?


    Qt Code:
    1. void RootGraphicsItem::layoutChildItems()
    2. {
    3. ...................................
    4.  
    5. ...................................
    6. x = 0.0;
    7.  
    8. //the first bounding rectangle is large ellipse's bounding rectangle
    9. // and the second one is the small ellipses bounding rectangle
    10. y = (boundingRect().height()/2.0) - (m_dockOutGraphicsItem->boundingRect().height()/2.0);
    11.  
    12.  
    13. m_dockOutGraphicsItem->setPos(x,-y);
    14.  
    15. ...................................
    16.  
    17. ...................................
    18. }
    To copy to clipboard, switch view to plain text mode 


    The arrangement is functional, but might be more accurate if i do the mapping before setting the position. since i am rendering in big ellipses coordinate system now, do i have to call mapToItem(...). I called it as follows:

    Qt Code:
    1. m_dockOutGraphicsItem->setPos(mapFromItem(m_dockOutGraphicsItem,x,-y));
    To copy to clipboard, switch view to plain text mode 

    Bu the effect is the same, so what is the use of this mapping and when to use it.

    A more explanation will be very helpful, any reference ?


    Regards
    Sajjad

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: coordinate transformation of graphics item

    It's a complex subject.
    If you haven't read The Graphics View Coordinate System I would suggest "Coordinate Mapping" there as a starting point.

Similar Threads

  1. Qt Coordinate System and the Graphics View Framework
    By djurodrljaca in forum Qt Programming
    Replies: 14
    Last Post: 17th February 2012, 11:19
  2. 2D Graphics on coordinate system
    By soumya in forum Qt Programming
    Replies: 0
    Last Post: 4th November 2009, 06:27
  3. Replies: 5
    Last Post: 27th September 2009, 12:55
  4. QGraphics Item Transformation inheritance
    By jsabater in forum Qt Programming
    Replies: 0
    Last Post: 14th August 2009, 13:26
  5. item local coordinate
    By dreamer in forum Qt Programming
    Replies: 6
    Last Post: 11th May 2008, 22:49

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.