简体   繁体   English

覆盖addAction()以修改pyqt中的字符串和QIcon

[英]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. 我想重写pyqt中的addAction()函数,以便每当在我的QMenu类中添加一个项时,就可以修改我的字符串和图标(如果未指定,则添加一个默认值)。 Here is the code : 这是代码:

Python code Python代码

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. 如您所见, QAction addAction(自身,QIcon图标,QString文本)可以将QIcon作为参数。 This is how I created my QMenu: 这就是我创建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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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