2

very early build ~ zip file

I am getting blank outputs for button images in a custom toolbar. Although the button information populates the dockable toolbar, they lack respective icons [see Fig.1]

Much of the following code was referenced from Add toolbar via Python

def initGui(self):
    # Add toolbar

    self.toolbar = self.iface.addToolBar("Graphic Groundwater")
    self.toolbar.setObjectName("Graphic Groundwater")

    # Create actions
    self.getDEM = QAction(QIcon(":/plugins/GraphicGroundwater/icons/getDEM.png"),
                           QCoreApplication.translate("Groundwater Modeling", "Get DEM Map"),
                           self.iface.mainWindow())
    self.getTopo = QAction(QIcon(":/plugins/GraphicGroundwater/icons/getTopo.png"),
                           QCoreApplication.translate("Groundwater Modeling", "Get Topo Map"),
                           self.iface.mainWindow())
    self.addBase = QAction(QIcon(":/plugins/GraphicGroundwater/icons/addBase.png"),
                           QCoreApplication.translate("Groundwater Modeling", "Add Base Area"),
                           self.iface.mainWindow())
    self.addNewBase = QAction(QIcon(":/plugins/GraphicGroundwater/icons/addNewBase.png"),
                           QCoreApplication.translate("Groundwater Modeling", "Add New Base Area"),
                           self.iface.mainWindow())
    self.addRiver = QAction(QIcon(":/plugins/GraphicGroundwater/icons/addRiver.png"),
                           QCoreApplication.translate("Groundwater Modeling", "Add River"),
                           self.iface.mainWindow())
    self.addStress = QAction(QIcon(":/plugins/GraphicGroundwater/icons/addStress.png"),
                           QCoreApplication.translate("Groundwater Modeling", "Add Stress"),
                           self.iface.mainWindow())
    self.addGrid = QAction(QIcon(":/plugins/GraphicGroundwater/icons/addGrid.png"),
                           QCoreApplication.translate("Groundwater Modeling", "Add Grid"),
                           self.iface.mainWindow())
    self.refreshGrid = QAction(QIcon(":/plugins/GraphicGroundwater/icons/refreshGrid.png"),
                           QCoreApplication.translate("Groundwater Modeling", "Refresh Attributes"),
                           self.iface.mainWindow())
    self.runModel = QAction(QIcon(":/plugins/GraphicGroundwater/icons/runModel.png"),
                           QCoreApplication.translate("Groundwater Modeling", "Run Model"),
                           self.iface.mainWindow())

    # Connect action signals to slots
    self.getDEM.triggered.connect(self.run)
    self.getTopo.triggered.connect(self.run)
    self.addBase.triggered.connect(self.run)
    self.addNewBase.triggered.connect(self.run)
    self.addRiver.triggered.connect(self.run)
    self.addStress.triggered.connect(self.run)
    self.addGrid.triggered.connect(self.run)
    self.refreshGrid.triggered.connect(self.run)
    self.runModel.triggered.connect(self.run)

    # Add actions to the toolbar
    self.toolbar.addAction(self.getDEM)
    self.toolbar.addAction(self.getTopo)
    self.toolbar.addAction(self.addBase)
    self.toolbar.addAction(self.addNewBase)
    self.toolbar.addAction(self.addRiver)
    self.toolbar.addAction(self.addStress)
    self.toolbar.addAction(self.addGrid)
    self.toolbar.addAction(self.refreshGrid)
    self.toolbar.addAction(self.runModel)

def unload(self):
    # remove toolbar on plugin unload
    del self.toolbar

[Fig.1] blank toolbar

The associated resource_rc file might also be of some diagnosis assistance...

<qresource prefix=":/plugins/graphicgroundwater/" >
    <file>GraphicGroundwater.png</file>
    <file>getDEM.png</file>
    <file>getTopo.png</file>
    <file>addBase.png</file>
    <file>addNewBase.png</file>
    <file>addRiver.png</file>
    <file>addStress.png</file>
    <file>addGrid.png</file>
    <file>refreshGrid.png</file>
    <file>runModel.png</file>
</qresource>

2 Answers 2

4

Assuming the basic directory structure of the uninstalled plugin is:

GraphicGroundwater
  icon.png
  icons
    addBase.png
    ...
  resources.qrc
  ...

You will want something like this in resources.qrc (see docs):

<qresource prefix="/plugins/GraphicGroundwater" >
    <file>icon.png</file>
    <file>icons/getDEM.png</file>
    <file>icons/getTopo.png</file>
    <file>icons/addBase.png</file>
    <file>icons/addNewBase.png</file>
    <file>icons/addRiver.png</file>
    <file>icons/addStress.png</file>
    <file>icons/addGrid.png</file>
    <file>icons/refreshGrid.png</file>
    <file>icons/runModel.png</file>
 </qresource>

Where icon.png = GraphicGroundwater.png and is your plugin's representative icon.

Notes:

  • The paths inside the <file> tags are relative to the .qrc file's directory. The prefix string just helps define a unique identifier to be used in the generated resources_rc.py file, where the icons themselves are embedded. Alternatively, you can skip a resource file and just define a system path to icons, but the use of a resources file is good form.

  • The colon is not needed in the resource.qrc file's prefix, nor the trailing slash. Use the colon when referencing the resource in your code, as you have correctly done in your QAction icon paths:

    QIcon(":<prefix>/<file>")
    # e.g. 
    QIcon(":/plugins/GraphicGroundwater/icons/addBase.png")
    
  • You will want to avoid using spaces in object names, e.g.:

    self.toolbar.setObjectName("graphic_groundwater")  # or graphicgroundwater
    
  • When setting a translation string, generally the first parameter is the parent class's name of the initGui function, e.g. GraphicGroundwater, if that is what it is:

    QCoreApplication.translate("GraphicGroundwater", "Get DEM Map")
    

There are a couple of other assumptions you may want to check, if above solution doesn't seem to work:

  • Verify your are generating the correct resources_rc.py module from your .qrc, using pyrcc4.

  • Verify you are importing the resultant module, e.g. import resources_rc.

7
  • Took your advice on all accounts for sake of streamlining things. Unfortunately, something in the build still seems to be preventing the images being referenced.
    – Katalpa
    Commented May 19, 2014 at 22:59
  • Would there be a conflict between the central file this code is excerpted from (graphicgroundwater.py), and the as of yet unedited (ui_graphicgroundwater.py)? When the blank toolbar buttons are selected, the ui properly opens, although it to is currently blank (yet to write). A novice idea on the cause of this error; is it possible the icon image resolutions would cause a 'no-show'? All the documentation I have looked through states the QGIS toolbars are happy with 24x24, which is what the resoucres in question are...
    – Katalpa
    Commented May 19, 2014 at 23:06
  • You will need to give more info on your project, or find a means of posting the project files. Are you indeed using a Makefile or other method of deploying the plugin to ~/.qgis/python/plugins/graphicgroundwater/? And in that file, are you using pyrcc4 to generate (then install) the resultant resources_rc.py?
    – dakcarto
    Commented May 19, 2014 at 23:33
  • The icon size of 24x24 if fine, though realize the QGIS icon interface spec goes up to 32x32. Your best bet is to create the icons as SVG 1.0 (e.g. in Inkscape) so that they are resolution-independent.
    – dakcarto
    Commented May 19, 2014 at 23:35
  • Thank you for your wisdom dakcarto. I will look into the SVG 1.0 soon, but priority will be to actually get the file structure stable and working! Compressed directory added for diagnosis at crown of posting. There is deffinatly a major amount of ongoing work on this...
    – Katalpa
    Commented May 20, 2014 at 12:22
0

I just discovered a solution, pretty straight forward:

I am using QGIS 2.6, and the plugin builder 2.0.3 -- both downloaded within the last couple of weeks.

The plugin builder creates a "resources.qrc" file, while creating a main script with this reference:
import resources_rc at line 27

Until I removed the trailing "_rc" in the main script, I also did not have an icon in the bar, but did have a blank spot [icon-less] which would run my test plugin. On correcting the main script import reference, the icon appeared. This fix also eliminated a QGIS startup error that appeared if the plugin had been successfully installed in an earlier run/instance of QGIS [an error that did not appear otherwise, later while running QGIS or the plugin]: more/less: failing to see a resources_rc.

May I suggest (to whomever) that either the file name or the import reference in the plugin builder be corrected, to match?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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