PDA

View Full Version : QGraphicsItemGroup childItems() returns only count()



kefir
19th July 2011, 16:44
Fellow Qt developers, I seek your wisdom!

I have this kind of code


void QGSLayer::updateFeatures(int zoom)
{
if(lyrType == DynamicLayer)
{
QGSCoordinateTransform ct;
ct.setZoom(zoom);

for(int i=0;i<this->childItems().count();i++)
{
QGraphicsItem *item = childItems().at(i);

QPointF ptG = ct.metersToLatLon(ct.pixelsToMeters(item->pos().toPoint()));

QPoint pt = coordTransform->metersToPixels(coordTransform->latLonToMeters(ptG));
item->setPos(pt);
}
}
}


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!

mcosta
19th July 2011, 16:57
QGraphicsItem::pos returns the position of item in "parent coordinates".

The value (0, 0) is the result of "pos()" or "toPoint()"?

kefir
19th July 2011, 20:31
Both. Even in parent coordinates they should not be 0,0
And as I understand toPoint just "rounds" coordinates and returns integer values...

mcosta
19th July 2011, 20:39
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?

kefir
19th July 2011, 22:50
It is posted)
I invoke this function in the graphicsview class while reading all layers. Like this:


for(int i=0;i<userLayers->childItems().count();i++)
{
QGSLayer *lyr = (QGSLayer *)userLayers->childItems().at(i);
lyr->updateFeatures(oldZoom);
}

mcosta
20th July 2011, 07:36
This is a piece of your code.



QGraphicsItem *item = childItems().at(i);

QPointF ptG = ct.metersToLatLon(ct.pixelsToMeters(item->pos().toPoint()));

QPoint pt = coordTransform->metersToPixels(coordTransform->latLonToMeters(ptG));
item->setPos(pt);


You
convert the position from scene coordinate to meters (using zoom probably)
convert meters to LatLong (using a Reference Point)
convert LatLong in meters
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?

kefir
20th July 2011, 10:59
Em... The initial position is hardly 0,0... Look at the picture attached. Black dots are points.

Sukesh
20th July 2011, 12:25
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

kefir
20th July 2011, 13:03
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)