10

I'm using this code to get features from QgsVectorLayer

 QgsVectorDataProvider* provider=theVectorLayer->dataProvider();
            if(!provider)
            {
                return;
            }

            theVectorLayer->select(provider->attributeIndexes(),theVectorLayer->extent(),true,false);
    theVectorLayer->selectedFeatureCount();//here returns 0 but there is one polygon
    while(theVectorLayer->nextFeature(currentFeature))
            {
         ....//here Iget attributes and current geometry (one polygon)
            }

How do I get feature count?

Method featureCount() returns -1.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
bossman
  • 233
  • 1
  • 3
  • 5

1 Answers1

15

When featureCount() returns -1 the only safe bet to get the feature count is to iterate over all feature and count them.

selectedFeatureCount() is the number of features that are selected on the layer (e.g. with setSelectedFeatures() or void select(QgsFeatureId featureId, bool emitSignal=true)), but has nothing to do with the select()/nextFeature() combo.

unicoletti
  • 7,339
  • 22
  • 33
jef
  • 2,717
  • 16
  • 18