1

I have constructed a QGIS plugin using QGIS Plugin Builder 3.0.3.

Within my plugin, I need to load a .png file as a Qicon object. What needs to be done for the plugin script to locate the .png file when it is run within QGIS?

For my situation, my .png file is called line_1x.png.

I have modified the resources.qrc within the plugin to show a reference to the icon as shown below:

<RCC>
<qresource prefix="/plugins/geomAttribute" >
    <file>attribute_table.png</file>
    <file>line_1x.png</file>
</qresource>

I compiled the resources file using the following command pyrcc5 -o resources.py resources.qrc.

Within my plugin python script I try to construct the QIcon object using: icon = QIcon('./line_1x.png')

However, the plugin python script can't access the line_1x.png file. When I use a full file path to the icon file instead of a relative path the icon image file is read properly. Hence, using the following does work: icon = QIcon('C:/Users/pippi/AppData/Roaming/QGIS/QGIS3/profiles/default/python/plugins/geomattribute/line_1x.png'

How do a I get a QGIS plugin to read an image for an icon using a relative path?

1

The relative path for a file in a plugin is:

:/plugins/PluginClassName/filename

If in doubt, search for the path used for the icon_path in the def initGui(self): method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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