1

I used the following code to label the line features(roads) in a layer.It works in the console, but not by python code(application) It works for the points too.

What did I do wrong?

 def labell(self):
    layer = self.iface.mapCanvas().currentLayer()
    palyrr = QgsPalLayerSettings()
    palyrr.readFromLayer(layer)
    palyrr.enabled = True
    palyrr.fieldName = 'name'
    palyrr.setDataDefinedProperty(QgsPalLayerSettings.Size,True,True,'08','')
    palyrr.writeToLayer(layer)
    layer.commitChanges()
    self.iface.mapCanvas().refresh()
2
  • Please could you provide more info? Do you are getting some error? What you mean "but not by python code"?, Are you trying it as an external standalone script? – lrssvt Nov 16 '14 at 16:00
  • I'm developing a plugin so I tried this in my code. It doesn't give an error. For line features-it enables all the settings but does not apply them to the layer. When I go and check layer properties->Label tab, it shows that the "Label this layer with name" is checked but I have to still press "OK" to apply them to the layer. I even added the codings "layer.commitChanges()" and "self.iface.mapCanvas().refresh()". But they didn't apply. But this code works perfectly and apply the labelling, when I copy and run in qgis console. What Should I do to apply changes? – Dil Nov 17 '14 at 1:11
2

I don't know what could be the difference between console and plugin, I think it should to work either way. Also you try to replace layer.commitChanges() (which makes not sense there) with layer.triggerRepaint() and look if that helps (note it is not necessary to call mapCanvas.refresh()). You can also use custom properties for the layer to enable labeling, a sample code is the following:

layer = iface.activeLayer()
layer.setCustomProperty("labeling/fieldName", "name" )
layer.setCustomProperty("labeling/placement", QgsPalLayerSettings.Line)
layer.setCustomProperty("labeling/fontSize","8" )
layer.setCustomProperty("labeling/enabled","true" )
layer.triggerRepaint()

EDIT

for linear features you have to set the placement property as well. I changed the above code. Your first posted code should also work by adding the following line:

palyrr.placement = QgsPalLayerSettings.Line

You can use other placement attributes please have a look at this link

3
  • I tried your code, but same as before. It works for points, but not for line features. – Dil Nov 17 '14 at 10:38
  • please check out if it works with the changes above. – lrssvt Nov 17 '14 at 12:17
  • @Dil Hi, I can't seem to get it to work. Just like Dil I manage to change the properties but still have to hit OK. I am using Irssvt's script. In addition: I am trying to add a buffer around my label but can't seem to get it to work. I tried by adding this to the script: layer.setCustomProperty("labeling/DataDefinedProperties",QgsPalLayerSettings.BufferDraw) – Hannes Ledegen Jan 6 '17 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.