QgsVectorLayer* layer = new QgsVectorLayer("path/to/layer.shp", "layerName", "ogr"); if (!layer->isValid()) { // error handling } QgsFeatureIterator it = layer->getFeatures(); while (it.hasNext()) { QgsFeature feature = it.next(); QVariantMap attributes = feature.attributes(); QStringList keys = attributes.keys(); foreach (const QString& key, keys) { qDebug() << key << ":" << attributes.value(key).toString(); } }
QgsVectorLayer* layer = new QgsVectorLayer("path/to/layer.shp", "layerName", "ogr"); if (!layer->isValid()) { // error handling } QgsFeatureRequest request; request.setFilterExpression("population >= 100000"); QgsFeatureIterator it = layer->getFeatures(request); while (it.hasNext()) { QgsFeature feature = it.next(); qDebug() << feature.id(); }In this example, we opened a vector layer, created a feature request object with a filter expression that selects only those features whose "population" attribute is greater than or equal to 100000, and then retrieved those features using getFeatures method. Package library: QGIS C++ library.