Results 1 to 8 of 8

Thread: Problem with QGraphicsItem coordinades

  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 Problem with QGraphicsItem coordinades

    Hi,

    I have few QGraphicsLineItems linked in a line one after the other. All of them have a QGraphicsItem as a parent, some "parents" have just one child and some few.

    I want to update the positions of each line eachtime the others are moved. Until now it worked fine using "mapFromItem" in the starting point and the last point of each line, with the coords of the following and the previous items.

    But when I create a new QGraphicsLineObject at the end of the queue, and if the previous parent object has been moved, the coordenade system is moved as well and "mapFromItem" is not right (it's moved a bit more, I thinks that's because the parent has been moved and its coordenade axis as well).

    I tried with "mapToScene", but it doesn't work fine:

    Qt Code:
    1. QPointF newPos = mapToScene(my_OLDItem->line().p2());
    2. my_NEWItem->setPos(NewPos); //my_NEWItem is a QGraphicsPixmapItem
    To copy to clipboard, switch view to plain text mode 

    what should I map, and where? any idea? thanks!

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

    Default Re: Problem with QGraphicsItem coordinades

    Are the items connected using parent-child relationship? If you wish to update their scene positions then they shouldn't be... It doesn't make much sense otherwise.
    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: Problem with QGraphicsItem coordinades

    Nop, the QGraphicsLineItem are connected graphically (the last point is in the same position of the first point of the next one).

    Thus, all of them are child of a QGraphicsPathItem. I have several of those items and all of them are parent of one or more QGraphicsLineItem.

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

    Default Re: Problem with QGraphicsItem coordinades

    So what's the problem? Reimplement itemChange() for your item and move all the associated item when the current item moves. If one item moved 10 pixels right and 7 down, all others have to move 10 pixels right and 7 down - you don't have to map anything. Furthermore if they have the same parent, they "live" in the same coordinate system which makes the whole thing even easier.
    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
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QGraphicsItem coordinades

    need some clarification ...
    just query ... why QGraphicsItemGroup cant do this ... when an item in the group moved the whole group items will move respectively ..?
    "Behind every great fortune lies a crime" - Balzac

  6. #6
    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: Problem with QGraphicsItem coordinades

    wysota: I forgot... I also have QGraphicsPixmap items, linked with the LineItems and inside the pathItems as well. So my big issue was with them.

    I was dealing with "setScenePos" and "mapToScene", but they didn't work... now I'm working with "setPos" and "mapToParent/mapFromParent" more or less succesfully.


    wagmare: that's because my program... I need to display a QPath and if I use a QGraphisGroup I need to create a QPath and calculate the points of the QPath manually when is moved and paint a qpath on the screen (because it moves the items inside, but the paints to be drawn as a QPath need to be calculated each frame and it's not worth). I do prefer to create a QGraphicsPathItem, move it as a normal item and move the items inside automatically because they are its childs.

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

    Default Re: Problem with QGraphicsItem coordinades

    Quote Originally Posted by jano_alex_es View Post
    wysota: I forgot... I also have QGraphicsPixmap items, linked with the LineItems and inside the pathItems as well. So my big issue was with them.

    I was dealing with "setScenePos" and "mapToScene", but they didn't work... now I'm working with "setPos" and "mapToParent/mapFromParent" more or less succesfully.
    If they have a common parent then you shouldn't be mapping to scene. There should be no mapping at all.

    In general it would be easiest to place all the movable items into one item group and make that group movable instead of the child items themselves. If you tell the parent to handle events from its children, it should work without any additional code. And no need to react on position changes.
    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.


  8. #8
    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: Problem with QGraphicsItem coordinades

    That works fine... but it does not when they are childs of different parent items.

    Imagine we have:

    QLineItem at position (general position, in a paper, not in qt) from 0,0 to 20,0

    QLineItem from 20, 0 to 40, 40

    QLineItem from 40, 40 to 80, -30

    all are childs of the same QItem

    and, after, connected, I have a QPixmapItem at pos 80,30, and it's a child of another QItem.

    I was having problems mapping these coordenades, mapToScene of the line().p2() and map from scene at QPixmapItem.

    Fortunatly, in my program each QPixmapItem needs to be surrounded by three qLineItem, being all of them childs of the same parent, so I can pass the coords mapfromItem between the qlineItems that are childs of different parents and map to parent between the items of the same parent.

Similar Threads

  1. PyQt QTimer problem { FIXED }
    By WinchellChung in forum Newbie
    Replies: 0
    Last Post: 1st March 2008, 16:50
  2. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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
  •  
Qt is a trademark of The Qt Company.