gpt4 book ai didi

python - 重写addAction()来修改pyqt中的字符串和QIcon

转载 作者:行者123 更新时间:2023-12-01 04:19:00 26 4
gpt4 key购买 nike

我想重写 pyqt 中的 addAction() 函数,以便每次在 QMenu 类中添加一项时都可以修改我的字符串和图标(如果未指定,则添加默认字符串和图标)。这是代码:

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

想要的结果是带有这些字符串(和图标)的菜单:

1. First thing
2. Second Thing

最佳答案

如您所见,QAction addAction (self, QIcon icon, QString text) 可以将 QIcon 作为参数。这就是我创建 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)

关于python - 重写addAction()来修改pyqt中的字符串和QIcon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33938006/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com