Results 1 to 9 of 9

Thread: QGraphicsItemGroup childItems() returns only count()

  1. #1
    Join Date
    Mar 2011
    Location
    Moscow, Russia
    Posts
    11
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QGraphicsItemGroup childItems() returns only count()

    Fellow Qt developers, I seek your wisdom!

    I have this kind of code
    Qt Code:
    1. void QGSLayer::updateFeatures(int zoom)
    2. {
    3. if(lyrType == DynamicLayer)
    4. {
    5. QGSCoordinateTransform ct;
    6. ct.setZoom(zoom);
    7.  
    8. for(int i=0;i<this->childItems().count();i++)
    9. {
    10. QGraphicsItem *item = childItems().at(i);
    11.  
    12. QPointF ptG = ct.metersToLatLon(ct.pixelsToMeters(item->pos().toPoint()));
    13.  
    14. QPoint pt = coordTransform->metersToPixels(coordTransform->latLonToMeters(ptG));
    15. item->setPos(pt);
    16. }
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    QGSLayer is subclassed from QGraphicsItemGroup. It contains different items that were successfully added to the scene and displayed.

    The problem is... childItems().count() returns correct amount of items but when I try to read their positions I always get 0, 0. But it isn't true (as you can see in the image attached). Maybe someone can explain this kind of behavior?

    Thanx in advance!
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItemGroup childItems() returns only count()

    QGraphicsItem::pos returns the position of item in "parent coordinates".

    The value (0, 0) is the result of "pos()" or "toPoint()"?
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Mar 2011
    Location
    Moscow, Russia
    Posts
    11
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsItemGroup childItems() returns only count()

    Both. Even in parent coordinates they should not be 0,0
    And as I understand toPoint just "rounds" coordinates and returns integer values...

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItemGroup childItems() returns only count()

    And as I understand toPoint just "rounds" coordinates and returns integer values...
    right

    Can you post the code where you set the item's position?
    Last edited by mcosta; 19th July 2011 at 20:39. Reason: updated contents
    A camel can go 14 days without drink,
    I can't!!!

  5. #5
    Join Date
    Mar 2011
    Location
    Moscow, Russia
    Posts
    11
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsItemGroup childItems() returns only count()

    It is posted)
    I invoke this function in the graphicsview class while reading all layers. Like this:
    Qt Code:
    1. for(int i=0;i<userLayers->childItems().count();i++)
    2. {
    3. QGSLayer *lyr = (QGSLayer *)userLayers->childItems().at(i);
    4. lyr->updateFeatures(oldZoom);
    5. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItemGroup childItems() returns only count()

    This is a piece of your code.

    Qt Code:
    1. QGraphicsItem *item = childItems().at(i);
    2.  
    3. QPointF ptG = ct.metersToLatLon(ct.pixelsToMeters(item->pos().toPoint()));
    4.  
    5. QPoint pt = coordTransform->metersToPixels(coordTransform->latLonToMeters(ptG));
    6. item->setPos(pt);
    To copy to clipboard, switch view to plain text mode 

    You
    1. convert the position from scene coordinate to meters (using zoom probably)
    2. convert meters to LatLong (using a Reference Point)
    3. convert LatLong in meters
    4. convert meters in scene coordinates.


    It's normal that, if the initial position is (0,0), the new one will be (0,0).

    Are you sure the algorithm you used is correct?
    A camel can go 14 days without drink,
    I can't!!!

  7. #7
    Join Date
    Mar 2011
    Location
    Moscow, Russia
    Posts
    11
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsItemGroup childItems() returns only count()

    Em... The initial position is hardly 0,0... Look at the picture attached. Black dots are points.

  8. #8
    Join Date
    Feb 2011
    Posts
    2
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QGraphicsItemGroup childItems() returns only count()

    Hi Kefir,

    you should use scenePos() to get the position of your items AFIK the pos() method will return 0,0 i.e origin of the item itself in local coordinates. Though Qt documentation says that a parent less item will return position coordinates in terms of Scene. From the code and snap shot it seems that you are developing a GIS application. I would appreciate if you could share some information on your application I am also into GIS application development.

    With regards
    Sukesh

  9. #9
    Join Date
    Mar 2011
    Location
    Moscow, Russia
    Posts
    11
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsItemGroup childItems() returns only count()

    Thank you, Sukesh!

    But I have doubts that pos() returns origin of the item, as it
    Returns the position of the item in parent coordinates. If the item has no parent, its position is given in scene coordinates.
    ... But I'll try thinking in this direction...

    Actually it is GIS API. So far I succesfully load google/openstreet/yandex and GeoServer tiles, zoom in/out, add features by coordinate. I'm afraid I can't give you my sources as this development is for my company and it is not opensource. However I answer some questions and give you links that may help you)

Similar Threads

  1. QGrapchisSvgItem childItems list empty
    By Hogwarts in forum Qt Programming
    Replies: 1
    Last Post: 6th April 2011, 12:18
  2. problem in QGraphicsItemGroup
    By wagmare in forum Qt Programming
    Replies: 1
    Last Post: 14th May 2009, 08:27
  3. Add two QGraphicsItemGroup
    By ashishsaryar in forum Qt Programming
    Replies: 2
    Last Post: 5th February 2008, 18:00
  4. QGraphicsItemGroup problem
    By init2null in forum Qt Programming
    Replies: 2
    Last Post: 20th November 2006, 22:45
  5. setZValue of QGraphicsItemGroup
    By forrestfsu in forum Qt Programming
    Replies: 3
    Last Post: 6th November 2006, 19:40

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.