1

I want to override the function addAction() in pyqt to have the possibility to modify my strings and icons (or add a default one if none has been specified) every time an item is added in my QMenu class. Here is the code :

Python code

class Menu(QtWidgets.QMenu):

    def __init__(self, parent=None):
        super(Menu, self).__init__(parent)

        self.addAction("First thing")
        self.addAction("Second Thing")

    def addAction(self, *__args):
        *Modify string and icon

the wanted result would be a menu with these strings (and an icon):

1. First thing
2. Second Thing
syedelec
  • 1,285
  • 2
  • 19
  • 30

1 Answers1

2

As you can see QAction addAction (self, QIcon icon, QString text) can have a QIcon as a parameter. This is how I created my QMenu:

_menu = QMenu()

#Define action
_add_action     = _menu.addAction(QIcon("images\add.png"),"Add")
_remove_action  = _menu.addAction(QIcon("images\remove.png"),"Remove")

#Asign events to actions
self.connect(_add_action, SIGNAL("triggered()"), self._add_handle)
self.connect(_remove_action, SIGNAL("triggered()"), self._remove_handle)
Garjy
  • 467
  • 5
  • 12