[GIS] Missing qgis icon resources for symbology dialog

pyqgissvg

I'm trying to call QgsRendererV2PropertiesDialog from a standalone Qgis python script. It mostly works, except there's some "unknown error" messages related to SVG files when I trigger the dialog:

Cannot open file ':/images/themes/default/sort.svg', because: Unknown error
Cannot open file ':/images/themes/default/sort.svg', because: Unknown error
Cannot open file ':/images/themes/default/symbologyAdd.svg', because: Unknown error
Cannot open file ':/images/themes/default/symbologyAdd.svg', because: Unknown error
[etc]

and the relevant buttons that should have those icons are blank. The dialog looks fine with no missing buttons when I run Qgis itself.

Here's a test script that fires up a symbology dialog that produces the error for me, and one line after it that also produces the error when trying to load an icon. The missing icons are under the symbology layer list, with functions like new layer, lock colour, move layer up/down etc:

from qgis.core import *
from qgis.gui import *
import sys
sys.path.append("/usr/share/qgis/python/plugins")
from PyQt4.QtCore import *
from PyQt4.QtGui import *

app = QgsApplication([], True)
app.setPrefixPath("/usr", True)
app.setThemeName("default")
app.initQgis()
print QgsApplication.showSettings() 

layer = QgsVectorLayer("point","point","memory")
if not layer.isValid():
    raise ValueError,"Layer not valid"

canvas=QgsMapCanvas()
dialog = QgsRendererV2PropertiesDialog(layer, QgsStyleV2.defaultStyle(), embedded=False)
dialog.exec_()

print QIcon(QgsApplication.iconPath( "symbologyAdd.svg" ) )

app.exitQgis()

Note that the settings printed out by QgsApplication.showSettings() are the same in this script and in Qgis. I'm thinking there's some app initialisation that I've not done. I can't find the resource file that has those icons in it. The icon path is the same in my script, namely: ":/images/themes/default/symbologyAdd.svg".

So, any solution?

Best Answer

Martin Dobias suggested on the qgis list that the resources are probably compiled into the QGis executable, and so not available to Python standalone scripts.

Solution is to download the QGis source, create a resource description file, build a python resource file and import that.

Here's subset.qrc for the icons I need:

<RCC>
  <qresource prefix="/images">
    <file>themes/default/locked.svg</file>
    <file>themes/default/mActionDuplicateLayer.svg</file>
    <file>themes/default/sort.svg</file>
    <file>themes/default/symbologyAdd.svg</file>
    <file>themes/default/symbologyDown.svg</file>
    <file>themes/default/symbologyRemove.svg</file>
    <file>themes/default/symbologyUp.svg</file>
    <file>themes/default/unlocked.svg</file>
  </qresource>
</RCC>

Download the QGis source and put this file in the images folder. Run

pyrcc4 subset.qrc >resources.py

and then copy resources.py to somewhere my script can get it. Then in my script I just import resources and all the icons appear.

Related Question