[GIS] How to change and display the plugins icon in the menu in QGIS

pyqgisqgis-2

I want to add multiple icons in the same file to display the plugins icons in the menu.

def initGui(self):
    action = self.add_action(   ':/plugins/Example/icons\icon1.png',
                                    text=self.tr(u'Example'),
                                    callback=self.ExampleMethod,
                                    parent=self.iface.mainWindow(),
                                    checkable=True,
                                    toolbar=self.ExampleToolbar)

        self.ExampleMethod = QgsMapTool(self.iface.mapCanvas())
        self.ExampleMethod.setAction(action)

        action = self.add_action(   ':/plugins/Example/icons\icon2.png',
                                    text=self.tr(u'Example1'),
                                    callback=self.ExampleMethod1,
                                    parent=self.iface.mainWindow(),
                                    checkable=True,
                                    toolbar=self.ExampleToolbar)

        self.ExampleMethod1 = QgsMapTool(self.iface.mapCanvas())
        self.ExampleMethod1.setAction(action)

but the problem is that the icons are not displaying.

I have used make clean and make commands and I have also updated the resources.qrc file.

What I have to do to display the icons in the menu?

Best Answer

You are mixing the two kinds of slash ("\" and "/") in your icon path. The escape sequence '\i' does not exist and it is interpreted as the simple character 'i'. For this reason the paths are not correct in:

.
.
.
    action = self.add_action(   ':/plugins/Example/icons\icon1.png',
.
.
.
    action = self.add_action(   ':/plugins/Example/icons\icon2.png',
.
.
.

and they are interpreted as:

:/plugins/Example/iconsicon1.png 

and

:/plugins/Example/iconsicon2.png 

respectively.

Try out this:

.
.
.
    action = self.add_action(   ':/plugins/Example/icons/icon1.png',
.
.
.
    action = self.add_action(   ':/plugins/Example/icons/icon2.png',
.
.
.

I hope this helps.

Related Question