2

Im loading a QgsVectorLayer and showing it on map. I load the style i prepared with desktop QGIS 2.0.1 using the layer.loadNamedStyles(path) function in the API and it seems to work, kind of, because the markers are shown on map canvas the way i specified, however the labels are not shown even though the labels styling is defined in the .qml style file (looked at it in notepad). Is there something else i need to do, to display the labels for a vector layer?

Result is the same if i save the style as .sld then load it via layer.loadSldStyle(path)

I think the issue is the rendering engine/settings used. The style files assumes 'pal' rendering in the custom properties, so it could be something with the way Pal related objects are (not) initialized for the layer

This seems to have enable the labels, but it just displays "label" for every feature:

layer.enableLabels(True)

I still think the style i prepared is being ignored for some reason. There must be a simpler/better way to do this!

If i use a qml file, generated with qgis 1.8, labels show up without me doing anything other than loading the layer. Must be 2.0.1 related.

UPDATE: I finally know what the issue is: .qml style file. Despite the fact that it was generated with qgis 2.0.1 it seems that when style file is loaded into my stand alone app, it tries to load the label styling information from the legacy part of the file, which is not filled out:

  <displayfield>name</displayfield>
  <label>0</label>
  <labelattributes>
    <label fieldname="" text="Label"/>
    <family fieldname="" name="MS Shell Dlg 2"/>
    <size fieldname="" units="pt" value="12"/>
    <bold fieldname="" on="0"/>
    <italic fieldname="" on="0"/>
    <underline fieldname="" on="0"/>
    <strikeout fieldname="" on="0"/>
    <color fieldname="" red="0" blue="0" green="0"/>
    <x fieldname=""/>
    <y fieldname=""/>
    <offset x="0" y="0" units="pt" yfieldname="" xfieldname=""/>
    <angle fieldname="" value="0" auto="0"/>
    <alignment fieldname="" value="center"/>
    <buffercolor fieldname="" red="255" blue="255" green="255"/>
    <buffersize fieldname="" units="pt" value="1"/>
    <bufferenabled fieldname="" on=""/>
    <multilineenabled fieldname="" on=""/>
    <selectedonly on=""/>
  </labelattributes>

If i changed the file by hand accordingly, the style now works. But the questions is: how do i force, or properly use custom properties where the style i saved was actually saved?

1

I had the same problem in a C++ application.
this sounds like the mapRenderer's labelingEngine of your canvas is not defined.

here is what i have done (in C++) :

#include <qgspallabeling.h>
...
QgsPalLabeling *labelingEngine = new QgsPalLabeling();
mCanvas->mapRenderer()->setLabelingEngine(labelingEngine);

I think you could do something like this in python :

from qgis.core import QgsPalLabeling
...
labelingEngine = QgsPalLabeling()
yourCanvas.mapRenderer().setLabelingEngine(labelingEngine)

with this, i have labels loaded properly using customProperties from qml.

  • Thank you very much, i was bumping my head on this. Its a shame there aren't many examples on this. Great new year present! +5 if i could ;) – U2ros Dec 31 '13 at 16:29

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.