PDA

View Full Version : MapToItem, MapToScene



mvbhavsar
28th January 2015, 11:26
Hello,

I am working on a graphics project where as there will parent QGraphicsRectItem. This QGraphicsRectItem will have 3 child QGraphicsEllipseItem. This ellipse needs to be arranged one after another in a row. When I am adding these ellipse item at runtime, its position get offset little bit. This is because I am not using any mapToItem, mapFromItem, mapToScene ot mapFromScene functions. Honestly speaking I am not cleared on the concept of these mapping fuctions. Can anybody please clear my fundamentals on these mapping functions and how I can use the same.



Thanks


Manish

d_stranz
29th January 2015, 01:47
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.

mvbhavsar
29th January 2015, 05:51
Thanks d_stranz for detailed update.
Yes, I understood this concept. What still remains the confusion is when to use mapToParent or mapToScene.

d_stranz
30th January 2015, 01:01
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.

mvbhavsar
30th January 2015, 08:18
Thanks a lot d_stranz.
It is very clear now. Very good explanation.

mustermann.klaus@gmx.de
20th November 2017, 21:28
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

d_stranz
21st November 2017, 19:04
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.

mustermann.klaus@gmx.de
22nd November 2017, 17:45
I'll try a new thread. Anyway, @d_stranz, if you read this: indeed a very good explanation.