Re: MapToItem, MapToScene
You don't need any of these to put child items inside a parent item.
Each item, no matter what it is, has its own coordinate system. Its coordinate system is used to draw its own contents. Each item is sized and positioned according to the coordinate system of its parent item (or the scene if it is a top-level item).
So if you have a QGraphicsRectItem with the size (100, 100), then its coordinate system goes from (0,0) to (99, 99). The top left corner is (0,0) and the bottom right corner is (99, 99). If you want to add a QGraphicsEllipseItem as a child, you make the child whatever size you want it to be (in the coordinate system of the parent rectangle), and place it inside the rectangle relative to the top left corner of the rectangle. The (0,0) coordinate for the ellipse is the top left corner of its boundingRect() (the rectangle that encloses the ellipse, not the rectangle you are using for its parent).
If you want to place an ellipse exactly in the center of the parent QGraphicsRectObject, you have to offset the ellipse's position by the half of the width and half of the height of its boundingRect().
Edit: Actually, I am not sure if the origin (0,0) of a QGraphicsItem is at the top left or at the center. Someone with a better memory might like to correct me if needed.
Re: MapToItem, MapToScene
Thanks d_stranz for detailed update.
Yes, I understood this concept. What still remains the confusion is when to use mapToParent or mapToScene.
Re: MapToItem, MapToScene
You use mapToParent() when you want to translate a position in your child widget into the corresponding position in its parent. You use mapToScene() when you want to translate a position from the child to the corresponding position in the scene.
For example, if you are holding a piece of paper in your hand, and you make a mark on the paper 10 cm down from the top and 5 cm from the left edge, that is the mark's position in child (paper) coordinates. If that piece of paper is in a big room, you can walk all around the room with the paper, but the child coordinates don't change. However, mapToParent() tells you where the mark is with respect to the room's dimensions (2 m from the North wall and 3 m from the West wall). When you walk around with the paper, the position in the room (i.e. parent coordinates) changes. If your scene is the building where the room is, then mapToScene tells you where the mark is with respect to the walls of the building, and likewise, walking around will change these coordinates too.
So you use mapToParent when you want to know where something in the child is in the parent's coordinate system, and you use mapToScene when you want to know where it is in the scene.
But, as is explained in the Qt documentation, a child object's position "pos()" is always in parent coordinates.
Re: MapToItem, MapToScene
Thanks a lot d_stranz.
It is very clear now. Very good explanation.
Re: MapToItem, MapToScene
Hi there, anybody still alive here? I tried really a long time, seems that I won't get it without any help.
Have a QChart with a QdateTimeaxis, embedded in a qwidget, which itself is centralwidged to a mainwindow.
A typical QPointF of a drawn QSeries would be p = QPointF(1.51122e+12, 4.1); // strange x-value due to qdatetime->toMsecSinceEpoche(); y-val common decimal value
Now I'd like to draw some additional information beside that point. I have stored that point separately, so mapFromScene() is already done.
I really tried all of the mapFrom's and mapTo's. Studied the "callout"-example. Seems that I'm blind on this spot.
I would wait to receive coordinates like QPoint(355, 277); but I don't. What am I doing wrong? What is the appropriate order of mapTo's and mapFrom's?
ANY help highly appreciatet.
Cheers, Lars
Re: MapToItem, MapToScene
You should be using QChart::mapToValue() with the real-world chart coordinates of the location. If your real-world coordinate is QPointF(1.51122e+12, 4.1), that's what you pass to the function. The QPointF that is returned is where you add your new QGraphicsTextItem with the additional information.
Re: MapToItem, MapToScene
I'll try a new thread. Anyway, @d_stranz, if you read this: indeed a very good explanation.