QgsVectorLayer* layer; // assume that this is a valid pointer to a vector layer QgsFeature feature(layer->fields()); feature.initAttributes(); // initializes the feature's attributes with default values layer->addFeature(feature); // adds the feature to the layer
QgsVectorLayer* layer; // assume that this is a valid pointer to a vector layer QgsFeature feature(layer->fields()); feature.initAttributes(); // initializes the feature's attributes with default values feature.setAttribute("name", "John Doe"); feature.setAttribute("age", 35); layer->addFeature(feature); // adds the feature to the layerIn this example, we create a new feature with the same fields as the layer, and call initAttributes() to initialize its attributes. Then we set specific values for two attributes using setAttribute(), and add the feature to the layer. Package/library: QGIS C++ API.