QgsVectorLayerLabelProvider class

The QgsVectorLayerLabelProvider class implements a label provider for vector layers.

Parameters for the labeling are taken from the layer's custom properties or from the given settings.

Base classes

class QgsAbstractLabelProvider
The QgsAbstractLabelProvider class is an interface class.

Derived classes

class QgsDxfLabelProvider
Implements a derived label provider internally used for DXF export.
class QgsRuleBasedLabelProvider

Public static functions

static auto getPointObstacleGeometry(QgsFeature& fet, QgsRenderContext& context, const QgsSymbolList& symbols) -> QgsGeometry
Returns the geometry for a point feature which should be used as an obstacle for labels.

Constructors, destructors, conversion operators

QgsVectorLayerLabelProvider(QgsVectorLayer* layer, const QString& providerId, bool withFeatureLoop, const QgsPalLayerSettings* settings, const QString& layerName = QString()) explicit
Convenience constructor to initialize the provider from given vector layer.

Public functions

void drawLabel(QgsRenderContext& context, pal::LabelPosition* label) const override
draw this label at the position determined by the labeling engine
auto labelFeatures(QgsRenderContext& context) -> QList<QgsLabelFeature*> override
Returns list of label features (they are owned by the provider and thus deleted on its destruction)
auto prepare(const QgsRenderContext& context, QSet<QString>& attributeNames) -> bool virtual
Prepare for registration of features.
void registerFeature(QgsFeature& feature, QgsRenderContext& context, const QgsGeometry& obstacleGeometry = QgsGeometry()) virtual
Register a feature for labeling as one or more QgsLabelFeature objects stored into mLabels.

Protected functions

void drawLabelPrivate(pal::LabelPosition* label, QgsRenderContext& context, QgsPalLayerSettings& tmpLyr, QgsTextRenderer::TextPart drawType, double dpiRatio = 1.0) const
Internal label drawing method.
void init()
initialization method - called from constructors

Protected variables

QgsCoordinateReferenceSystem mCrs
Layer's CRS.
QgsFields mFields
Layer's fields.
QList<QgsLabelFeature*> mLabels
List of generated.
QgsWkbTypes::GeometryType mLayerGeometryType
Geometry type of layer.
bool mOwnsSource
Whether layer's feature source is owned.
QgsPalLayerSettings mSettings
Layer's labeling configuration.
QgsAbstractFeatureSource* mSource
Layer's feature source.

Function documentation

static QgsGeometry QgsVectorLayerLabelProvider::getPointObstacleGeometry(QgsFeature& fet, QgsRenderContext& context, const QgsSymbolList& symbols)

Returns the geometry for a point feature which should be used as an obstacle for labels.

Parameters
fet point feature
context render context
symbols symbols rendered for point feature

This obstacle geometry will respect the dimensions and offsets of the symbol used to render the point, and ensures that labels will not overlap large or offset points.

bool QgsVectorLayerLabelProvider::prepare(const QgsRenderContext& context, QSet<QString>& attributeNames) virtual

Prepare for registration of features.

Parameters
context render context.
attributeNames list of attribute names to which additional required attributes shall be added
Returns Whether the preparation was successful - if not, the provider shall not be used

Must be called after provider has been added to engine (uses its map settings)

void QgsVectorLayerLabelProvider::registerFeature(QgsFeature& feature, QgsRenderContext& context, const QgsGeometry& obstacleGeometry = QgsGeometry()) virtual

Register a feature for labeling as one or more QgsLabelFeature objects stored into mLabels.

Parameters
feature feature to label
context render context. The QgsExpressionContext contained within the render context must have already had the feature and fields sets prior to calling this method.
obstacleGeometry optional obstacle geometry, if a different geometry to the feature's geometry should be used as an obstacle for labels (e.g., if the feature has been rendered with an offset point symbol, the obstacle geometry should represent the bounds of the offset symbol). If not set, the feature's original geometry will be used as an obstacle for labels.