3

I have a problem using the internal symbols/icons in my plugin. For example I want to use the "save" Symbol in a QToolButton or the "cancel" symbol in a QPushButton.

I designed the GUI in QT 5 Designer. I also added the symbols to the button. After I compiled the ui file with pyuic5 I changed the absolute path to the symbols to the relative path in my plugin folder after putting the downloaded svg files (like its discribed here and there) in the plugin directory.

Plugin folder overview

Running the file QgsAlkOsmCompare_dialog.py in the terminal, produces this: All icons show up in their correct position (I cutted the screenshot because of its size).

gui via terminal

Opening my plugin in QGIS is functional, but the icons disappear. Even if I run the same py file in the ./QGIS/QGIS3/profiles/default/python/plugins folder the plugin doesn't show the icons. I tried it with png file instead but nothing changed.

gui in qgis

Is it impossible to show buttons with icons in QGIS? I guess no, because I have seen icons in some of the GDAL processing functions too.

Can I import these internal QGIS icons in my GUI file or is it compiled to the qgis.exe/qgis or some other kind of resource file?

2

After a lot of experimentation I finally found a solution!

For some reason, the relative path to the icons was the Problem.

So you need a implement a method, to get the absolute path to the folder which includes the icons. I found a simple and quick solution in the Plugin QuickOSM by Etienne Trimaille.

def resources_path(*args):
        """Get the path to our resources folder.

        :param args List of path elements e.g. ['img', 'logos', 'image.png']
        :type args: str

        :return: Absolute path to the resources folder.
        :rtype: str
        """
        path = dirname(dirname(__file__))
        path = abspath(join(path, pardir, 'resources'))
        for item in args:
            path = abspath(join(path, item))

        return path

You call the method while initializing the icon for the Object:

...
self.button_start = QtWidgets.QPushButton(*yourQDialogName*)
icon = QtGui.QIcon(tools.resources_path("start.png"))
self.button_start.setIcon(icon)
...

Depending to your own folder structure you have to figure out what to use from pardir, abspath and dirname.

This solution worked for me on my Linux Mint 19.1 and Windows 8.1 Pro.

So, thanks o lot, Etienne Trimaille!

  • 1
    Nice I could help you. Credits go to the InaSAFE project where I copy/pasted this code. IMHO, it's always a pain for me to use pyuic to compile the file. Using this code, no need to compile the file everytime you change one icon. – etrimaille Apr 13 at 13:24

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.