Possible Duplicate:
How to style a layer from console?

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?

Thank you!


#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('',[])
# here myRenderer has still no categories!

# assign myRenderer to the active layer
layer = qgis.utils.iface.activeLayer()

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


In my plugin I use it like this:


  • looking in the source I also found layer.readSymbology(myDocRoot,errmsg) which also works fine and has a xml-element as parameter. – greenland Sep 14 '12 at 8:44

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