Results 1 to 16 of 16

Thread: A Object inheriting from QGraphicsLineItem

  1. #1
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default A Object inheriting from QGraphicsLineItem

    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!
    Last edited by jano_alex_es; 13th July 2009 at 09:46.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A Object inheriting from QGraphicsLineItem

    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: A Object inheriting from QGraphicsLineItem

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A Object inheriting from QGraphicsLineItem

    Quote Originally Posted by jano_alex_es View Post
    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().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: A Object inheriting from QGraphicsLineItem

    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

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A Object inheriting from QGraphicsLineItem

    Quote Originally Posted by jano_alex_es View Post
    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
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: A Object inheriting from QGraphicsLineItem

    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!

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A Object inheriting from QGraphicsLineItem

    Make your lines always start at origin and use setPos() to move them to appropriate positions.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    jano_alex_es (13th July 2009)

  10. #9
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: A Object inheriting from QGraphicsLineItem

    perfect, I'll try, thanks!

  11. #10
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: A Object inheriting from QGraphicsLineItem

    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).

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A Object inheriting from QGraphicsLineItem

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #12
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: A Object inheriting from QGraphicsLineItem

    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.



    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.

  14. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A Object inheriting from QGraphicsLineItem

    What's the code for generating the two arrows?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #14
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: A Object inheriting from QGraphicsLineItem

    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).

  16. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A Object inheriting from QGraphicsLineItem

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #16
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: A Object inheriting from QGraphicsLineItem

    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!!

Similar Threads

  1. Getting std::string object from QString object ( qt3)
    By joseph in forum Qt Programming
    Replies: 11
    Last Post: 28th March 2013, 20:09
  2. Replies: 4
    Last Post: 19th February 2009, 11:10
  3. Help with Q_PROPERTY with object pointer
    By lni in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2009, 17:31
  4. A form returning an object.
    By cbarmpar in forum Qt Programming
    Replies: 3
    Last Post: 8th September 2008, 05:21
  5. Open a QMainWindow Object in QDialog Object
    By chuengchuenghq in forum Qt Programming
    Replies: 1
    Last Post: 13th June 2008, 06:33

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.