PDA

View Full Version : A Object inheriting from QGraphicsLineItem



jano_alex_es
13th July 2009, 09:41
I have class which inherits from QGraphicsLineItem, I did that in order to have its methods and events (I need to inherit from other class, so I can´t create directly a GraphicsItem)

When I create an object, I send the line coordinates. But when is created, it pos is always 0,0. If I create five differents objects with different line coordinates, they are displayed correctly in the scene but they're pos() always give me back 0,0.

On the other hand, when I drag and drop one of the object its pos is changed according to its starting 0,0 coordinate, but the line().pos() gives me back the starting coords.


When I think on it, both functions should act differently... "pos()" giving me back the scene position and "line" the scene position of the line (but it should be the same, or similar, to "pos").

do it has any explanation? or is normal because I'm not working with the item itself?

thanks!

wysota
13th July 2009, 09:54
The "anchor" (pos) of the item is in its parent's (0,0) coordinates by default and is mapped to origin of the item. Now the item itself doesn't have to "contain" the (0,0) point, for instance a line can begin at point (10,100) and end at (100, 150) so it won't cross the origin of the coordinate system. The line will be drawn relative to its position. If you move the line item, you move its whole coordinate system (effectively changing pos) but the line still begins at (10, 100) and ends at (100,150) relative to its origin. line() gives you the coordinates of the line relative to the item's coordinate space, not the scene coordinate space. pos() gives you origin (coordinate space) of the item relative to the item's parent or scene if it has no parent. You have to add the two (or more if the item has a parent) transformations together to map between the world (scene) and your eyes (view).

jano_alex_es
13th July 2009, 10:20
line() gives you the coordinates of the line relative to the item's coordinate space, not the scene coordinate space

It explains why the (line)coords do not change when I move the item... :)



The "anchor" (pos) of the item is in its parent's (0,0) coordinates by default and is mapped to origin of the item

QT Assistant: Returns the position of the item in parent coordinates. If the item has no parent, its position is given in scene coordinates.

Ok, so when I create a new object, they will always have pos == 0,0; and if I move the item around the pos will give me back the position relative to its starting 0,0 pos. But my objects have no parents at all, so they should give back the scene position... but, if so, I can transform into scene coordinates with mapToScene.

wysota
13th July 2009, 10:59
Ok, so when I create a new object, they will always have pos == 0,0;
Yes, by default origins of both items (the item and its parent) match.


and if I move the item around the pos will give me back the position relative to its starting 0,0 pos.
No, it will give you the position relative to the parent. If you move the parent, the child will be moved too so its scene position will change but the value returned by pos() will not.


But my objects have no parents at all, so they should give back the scene position...
They should return position relative to the scene, not the scene position :)

but, if so, I can transform into scene coordinates with mapToScene.
Yes, or you can use QGraphicsItem::scenePos() and QGraphicsItem::sceneBoundingRect().

jano_alex_es
13th July 2009, 11:24
Yes, by default origins of both items (the item and its parent) match.

Ok, it has sense... but "scenePos" instead of just "pos" brings me back 0,0 as well :S

So, definitly, my object's parent is the scene... being different items into the same scene, shouldn't they have different coordinates?

I don't get it... seems to be monday :P

wysota
13th July 2009, 11:41
Ok, it has sense... but "scenePos" instead of just "pos" brings me back 0,0 as well :S
Because your item has no parent so the scene acts as one.


So, definitly, my object's parent is the scene... being different items into the same scene, shouldn't they have different coordinates?

No, why? Pauli exclusion principle doesn't apply here :)

jano_alex_es
13th July 2009, 12:34
mmh... so... what do I need to do to have all my items in the scene with the same coord system? where 30,30 (for example) is the same point in the screen for all of them.

I can call the scene coordenates and add or substract its initial point... but I'd prefer other way, if possible...

thanks!

wysota
13th July 2009, 12:50
Make your lines always start at origin and use setPos() to move them to appropriate positions.

jano_alex_es
13th July 2009, 13:31
perfect, I'll try, thanks!

jano_alex_es
13th July 2009, 14:29
The scene represents the base coordinate system for all its items. The scene coordinate system describes the position of each top-level item, and also forms the basis for all scene events delivered to the scene from the view

This is what QtAssistant says... what I understand there is that inside the same scene, there shouldn't be two items, in the same position, but in different position once they are displayed... I mean, the variable says they are in the same position but graphically they are in different places (and always inside the same scene).

wysota
13th July 2009, 15:29
Hmm? Why not? If they have different bounding rectangles then they will be in different scene positions. Especially that there are other transformations (like scaling and rotation) involved.

jano_alex_es
14th July 2009, 11:46
Yes, that's clear :P my problem is, for instance: these two arrows are in the same scene but their scene positions are the same.

If I understand right, The scene represents the base coordinate system for all its items. The scene coordinate system describes the position of each top-level item, and also forms the basis for all scene events delivered to the scene from the view I have a problem because what's happening in my code is not possible.

http://img194.imageshack.us/img194/980/arrowsz.jpg

More or less I solved it with mapToScene and mapFromScene... but when I try something more complicated it becomes crazy:

For example, I have two connected arrows (the finish of the first is aiming to the beginning of the second), they are different items so I get their coordinates with mapToScene and, from another object, I paint them using their coordinates (two QPoints for each arrow, the starting point and the finishing point, four in total) and mapFromScene.

Ok, this works fine... but when I create a QGraphicsItemGroup and I add both arrows, when I move that group the beginning of the third point (the starting point of the second arrow) becomes crazy again (the other three work fine, that's what I don't understand).

If I don't move the group, the coords are right. That's why I think I have done something strange in my code and my scene is not working properly.

wysota
14th July 2009, 14:17
What's the code for generating the two arrows?

jano_alex_es
15th July 2009, 08:13
it's just an example that happens sometimes... the code is quite extended and the problem should be hidden somewhere (please note the item come into another items sometimes, they change their parents and are saved and loaded few times... so just "paste" the problematic code seems impossible to me). I'd like to know if this is normal or, as I suppose, it has no sense at all (so I'll start to dive and find the problem).

wysota
15th July 2009, 09:14
I can't say if something is normal or not if I don't know what you did :) It seems you are overcomplicating things somewhat but that is just an impression, I can't be certain without seeing what you did.

jano_alex_es
15th July 2009, 09:25
I'm more than 200% sure I'm overcomplicating things :P I'm very good on that! ^^

Anyway, now it works... cheating a little bit (forcing coordinates in other functions) but it works; the point seems to be that I don't really understand how the scene system coordinates work, and this is something out of the scope of the forum; I'll find a tutorial somewhere.

thanks!!