[QGIS-Developer] Confused by QGIS 3 Labeling

Martin Dobias wonder.sk at gmail.com
Thu Jul 6 08:55:15 PDT 2017


Hi Calvin

On Thu, Jul 6, 2017 at 5:06 PM, C Hamilton <adenaculture at gmail.com> wrote:
> Formally I was using the following in my code to add a default label to my
> points vector layer:
>
>             label = QgsPalLayerSettings()
>             label.readFromLayer(ptLayer)
>             label.enabled = True
>             label.fieldName = 'label'
>             label.placement= QgsPalLayerSettings.AroundPoint
>             label.writeToLayer(ptLayer)
>
> In looking at the api break documentation readFromLayer and writeToLayer
> have been removed and I need to somehow use QgsAbstractVectorLayerLabeling,
> but I cannot see what I need to change just to add a label to my point
> layer. I am not trying to do anything fancy. All I want is to specify a
> field to be used to label the points layer and just use the default styling.
> Any help understanding the new API changes would be appreciated.

This should work:

settings = QgsPalLayerSettings()
settings.fieldName = 'id'
labeling = QgsVectorLayerSimpleLabeling(settings)
layer.setLabeling(labeling)

Please note that it is still too early to port your code to QGIS 3.0 -
there may be further API breaks. Here I would say that
QgsPalLayerSettings class may be renamed and its API may be updated.

Regards
Martin


More information about the QGIS-Developer mailing list