QgsVectorLayer* layer = new QgsVectorLayer("path/to/layer", "layer_name", "ogr"); QgsField populationField("Population", QVariant::Int); layer->pendingFields().append(populationField); layer->updateFields();
QgsVectorLayer* layer = new QgsVectorLayer("path/to/layer", "layer_name", "ogr"); QgsVectorLayerJoinInfo joinInfo; joinInfo.joinLayerId = "path/to/join/layer"; joinInfo.joinFieldName = "id"; joinInfo.targetFieldName = "join_id"; layer->addJoin(joinInfo); QListIn this code, we add a join to the vector layer and retrieve the fields for the join operation from the layer's pendingFields list. We then iterate over the fields and add them to the layer as new attributes before calling updateFields() to update the layer. The package library for QgsVectorLayer is the QGIS API.joinFields = layer->pendingFields(); for (const QgsField& field : joinFields) { layer->addAttributes({field}); } layer->updateFields();