[Qgis-developer] Troubles with features (QgsVectorLayer)

Matthias Kuhn matthias.kuhn at gmx.ch
Tue Nov 5 00:00:34 PST 2013


Hi Hubbatov,

Negative ID's are temporary, while Features are in an un-committed 
state and get replaced by real ones generated by the Provider as soon 
as you call commitChanges().

Regards,
Matthias

On Die 05 Nov 2013 08:45:33 CET, hubbatov wrote:
> Try this code. When i add feature to layer it says to me that id is negative
> and not depend from layer (-2, -3, -4... for first and -6,-7,-8... for
> second layer), but feature iterator says that ids are positive (1, 2, 3 ..
> for first layer and 1, 2, 3 ... for second layer).
>
> Is this a bug?
> What i`m doing wrong?
>
>
>         QgsVectorLayer *layer1 = new QgsVectorLayer("Polygon", "test1",
> "memory");
> 	QgsSingleSymbolRendererV2 *renderer1 = new
> QgsSingleSymbolRendererV2(QgsSymbolV2::defaultSymbol(layer1->geometryType()));
> 	layer1->setRendererV2(renderer1);
> 	map->addLayer(layer1);
> 	layer1->enableLabels(true);
>
> 	QgsVectorLayer *layer2 = new QgsVectorLayer("Polygon", "test2", "memory");
> 	QgsSingleSymbolRendererV2 *renderer2 = new
> QgsSingleSymbolRendererV2(QgsSymbolV2::defaultSymbol(layer2->geometryType()));
> 	layer2->setRendererV2(renderer2);
> 	map->addLayer(layer2);
>
>
> 	layer1->startEditing();
> 	QgsFeatureIds ids1;
> 	for(int i = 0; i < 5; i++){
> 		QgsFeature f;
> 		f.setGeometry(QgsGeometry::fromRect(QgsRectangle(i * 10, i * 10, (i + 1) *
> 10, (i + 1) * 10)));
> 		f.setValid(true);
>
> 		layer1->addFeature(f);
> 		ids1 << f.id();
> 	}
> 	layer1->commitChanges();
>
> 	layer2->startEditing();
> 	QgsFeatureIds ids2;
> 	for(int i = -1; i > -6; i--){
> 		QgsFeature f;
> 		f.setGeometry(QgsGeometry::fromRect(QgsRectangle(i * 10, i * 10, (i + 1) *
> 10, (i + 1) * 10)));
> 		f.setValid(true);
>
> 		layer2->addFeature(f);
> 		ids2 << f.id();
> 	}
> 	layer2->commitChanges();
>
>
> 	QgsFeature f;
> 	QgsFeatureIterator iter = layer1->getFeatures();
> 	while(iter.nextFeature(f)){
> 		qDebug() << "Available feature (layer1):" << f.id();
> 	}
> 	iter = layer2->getFeatures();
> 	while(iter.nextFeature(f)){
> 		qDebug() << "Available feature (layer2):" << f.id();
> 	}
>
> 	layer1->startEditing();
> 	foreach (QgsFeatureId id, ids1) {
> 		qDebug() << "Added feature (layer1):" << id;
> 	}
> 	layer1->commitChanges();
>
> 	layer2->startEditing();
> 	foreach (QgsFeatureId id, ids2) {
> 		qDebug() << "Added feature (layer2):" << id;
> 	}
> 	layer2->commitChanges();
>
>
>
> --
> View this message in context: http://osgeo-org.1560.x6.nabble.com/Troubles-with-features-QgsVectorLayer-tp5087389.html
> Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer




More information about the Qgis-developer mailing list