Hi, I'm new to QT Graphics and is really confused with its coordinate system.
I'm also not sure how parent & child items relate. Below is a simplified code to illustrate my questions.

1.) In the code I created a rectangle item m_RectItem1 from the scene object. Now when I check retangle1's position using pos(), it's giving me (0,0).Why is this? Shouldn't it be (100,100)? Documentation says that pos() is parent's coordinate .It appears that on each item that is added in the scene, pos() is always (0,0) which is not how the output looks like (which is obviously 100,100)

2.)I created another rectangle item m_RectItem2's and then set it's parent to m_RectItem1. I then set it's position to (0,0). From what I understood in the documenation, the child should be centered on it's parent's (0,0), however this is not the case, m_RectItem2 is still drawn outside its parent m_RectItem1. Can anyone explain?

Will greatly appreciate any enlightenment on these questions. Thanks!

Qt Code:
  1. GraphicsTestApp::GraphicsTestApp(QWidget *parent, Qt::WFlags flags)
  2. : QMainWindow(parent, flags)
  3. {
  4. ui.setupUi(this);
  5.  
  6. m_Scene = new QGraphicsScene();
  7. m_Scene->setSceneRect ( 0, 0, 500, 500 ) ;
  8. m_RectItem1 = m_Scene->addRect(QRectF(100,100,50,100));
  9. m_RectItem2 = m_Scene->addRect(QRectF(10,10,10,10));
  10. m_RectItem2->setParentItem(m_RectItem1);
  11. m_RectItem2->setPos(0,0);
  12.  
  13. qDebug() << m_RectItem1->scenePos();
  14. qDebug() << m_RectItem1->pos();
  15. qDebug() << m_RectItem1->boundingRect();
  16.  
  17. qDebug() << m_RectItem2->scenePos();
  18. qDebug() << m_RectItem2->pos();
  19. qDebug() << m_RectItem2->boundingRect();
  20.  
  21.  
  22. ui.graphicsView->setScene(m_Scene);
  23. }
To copy to clipboard, switch view to plain text mode