4

I'm trying to set the styles for a vector layer by loading the .qml file in the python console. The qml-style file contains all categories and symbols needed, but I can not find the right function to load the qml file. The QgsCategorizedSymbolRendererV2::create(domElement) seems to be not available in the python console (QGIS 1.8.0).

Is myRenderer.load(doc.documentElement()) the right function to use?

#load the xml
xmlfile = QtCore.QFile("qgis-layer-style.qml")
doc = QtXml.QDomDocument( "Categories" ) 
if xmlfile.open(QtCore.QIODevice.ReadWrite | QtCore.QIODevice.Text) == 0:
    print "cannot open file"
if doc.setContent( xmlfile ) == 0:
    print "no xml content"

creating a new renderer

myRenderer = QgsCategorizedSymbolRendererV2('',[]) myRenderer.load(doc.documentElement())

here myRenderer has still no categories!

assign myRenderer to the active layer

layer = qgis.utils.iface.activeLayer() layer.setRendererV2(myRenderer)

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
greenland
  • 49
  • 1
  • 2

1 Answers1

6

I have used this function from within a plugin. You could try it from the python console:

http://www.qgis.org/api/classQgsMapLayer.html#a6b117fdc1a090d014374a6981405d1ea

In my plugin I use it like this:

self.layer_name.loadNamedStyle('path_to_qml_file')
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
James S
  • 4,264
  • 2
  • 32
  • 46