简体   繁体   中英

Override addAction() to modify string and QIcon in pyqt

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM