Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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!

Stefan

#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)
share|improve this question

marked as duplicate by underdark Sep 14 '12 at 8:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

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')

share|improve this answer
    
Thank you! That's working and is easier, too! –  greenland Sep 14 '12 at 8:25
    
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.