#include "qgsvectorlayer.h" #include "qgsfield.h" // create a new vector layer with 3 fields QgsVectorLayer* layer = new QgsVectorLayer("Point", "MyLayer", "memory"); layer->dataProvider()->addAttributes( QList() << QgsField("Name", QVariant::String) << QgsField("Population", QVariant::Int) << QgsField("Latitude", QVariant::Double)); layer->updateFields();
#include "qgsvectorlayer.h" // create a new vector layer from a shapefile QgsVectorLayer* layer = new QgsVectorLayer("/path/to/shapefile.shp", "MyLayer", "ogr"); // get the features from the layer QgsFeatureIterator it = layer->getFeatures(); while (it.hasNext()) { QgsFeature f = it.next(); // do something with the feature qDebug() << f.attribute("Name").toString(); }This example demonstrates how to read a vector layer from a shapefile using the QgsVectorLayer constructor and getFeatures() method to retrieve the features from the layer. We then iterate over the features, print the "Name" attribute of each feature using attribute() method. These examples use the QGIS C++ API library.