Adding images to the GUI of your custom QGIS 2.X plugin with Qt Designer

10 Apr 2017 ᛫ 2 min read

SHARE:

Sometimes we want or need to add images to our custom QGIS plugins, here’s how you do it.

It’s highly suggested that you use images that are already in the size that you want them to be in your plugin

Add the images you want to your resources.qrc file

Make sure that resources.qrc is located in the root folder of your plugin.

<RCC>
    <qresource prefix="plugins/name-of-plugin" >
        <file>location/of/file1.png</file>
        <file>location/of/file2.png</file>
    </qresource>
</RCC>

Add the resources.qrc file to your GUI

Go to the Resource Browser located at the bottom right part of Qt Designer and click Edit Resources. Qt Designed Edit Resources QGIS plugin

Click Open Resource File and select the resources.qrc file you created.
Qt Designer add resources.qrc file QGIS plugin

If there are no errors, you should be able to see the resources(images) that you added in the resources.qrc file. Click OK. Qt Designer resources added QGIS plugin

Use Labels to hold the images in your GUI

Add a Label widget. Delete the text.

On the Property Editor, go to QLabel -> pixmap and select Select Resource. Add image as label in QGIS plugin GUI Qt Designer

Select the image you want to add and edit the size of the Label on your GUI. Image added as label in QGIS plugin GUI Qt Designer

Compile your resources.qrc to resources_rc.py

pyrcc resources.qrc -o resources_rc.py

If you want to know why we compile it to resources_rc.py, check here.

Compile your *.ui files to *.py files

sudo python /usr/lib/python2.7/dist-packages/qgis/PyQt/uic/pyuic.py name-of-file.ui -o name-of-file.py

After this, you should be able to see the images you added in your GUI the next time you run your plugin in QGIS.

SHARE:
comments powered by Disqus

You may also like:

Custom splash screens in QGIS

05 Sep 2020 ᛫ 2 min read

Run QGIS processing algorithms from the command line

17 Jul 2020 ᛫ 3 min read

Why you shouldn't use QGIS

22 Oct 2019 ᛫ 1 min read

Geotagged photos in QGIS

22 Sep 2019 ᛫ 4 min read

Bivariate choropleth maps in QGIS

15 Sep 2019 ᛫ 5 min read

BNHR

[email protected]

Creative Commons License
Except when explicitly stated otherwise, this work and its contents by Ben Hur S. Pintor is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Other works (software, source code, etc.) referenced in this website are under their own respective licenses.
This site is powered by Jekyll and hosted on Github (view source)