How to label only selected features in QGIS 2.8 and up

In 2011, I wrote “How to Label Only Selected Features in QGIS” which ends with the wish that

Another “data defined setting” like “show this label (true/false)” would be more intuitive.

… and now we have it!

It’s called Show label and you can find it in the Rendering section of the labeling dialog.

The following screenshot shows a quick example of how to label only airports starting with A by setting the expression



This post was motivated by a question by Eduardo here on this blog. Hope it helps!

  1. Eduardo Passos said:

    Thanks for answer my question, Anita!
    It really helped a lot.

    See ya. =D

  2. This is great, thank you very much for all of your work on QGIS and answering people’s questions. Would you be willing/able to link to a syntax resource for the expression string builder?

    • Thanks Valerie! The right-hand side of the expression dialog shows help and examples for all available functions and variables.

  3. Thanks for your post. I’m trying to make the same with PyQgis using PAL, but I don’t know how to make it. Could you show me the way?

    This is my code:

    from PyQt4.QtGui import QColor
    layer = iface.activeLayer()
    palyr = QgsPalLayerSettings()
    palyr.enabled = True
    palyr.bufferDraw= True
    palyr.bufferColor= QColor(“white”)
    palyr.bufferSize = 1
    palyr.scaleVisibility = True
    palyr.isExpression = True
    # code is a fieldname
    palyr.fieldName = ‘right( code ,3 )’
    # palyr.fontMinPixelSize = 20
    palyr.size = 20
    # data defined label for query “code not like ‘%000’ ¿?
    # next line is wrong
    #palyr.setDataDefinedProperty(QgsPalLayerSettings.dataDefinedValue, True, True, ‘”code” NOT LIKE \’%000\”, ”)
    palyr.textColor = QColor(“red”)
    palyr.drawLabels = True
    palyr.placement= QgsPalLayerSettings.OverPoint

%d bloggers like this: