传递调用triggered.connect()的QAction对象作为我单击QAction后触发的函数中的参数

2024-04-13

我正在使用 for 循环创建 QAction 对象列表,如下所示:

class some_class:
  self.tabs = []

  for self.i in range(0,10):
    self.tabs[self.i] = QtGui.QAction("New", self)
    self.tabs[self.i].triggered.connect(self.some_function)

  def some_function(self):
    print self.i

每当我单击创建的任何选项卡时,它只会触发选项卡[9]并仅打印“9”。

那么如何在触发 some_function() 的 some_function 中传递 QAction 对象本身


将索引缓存为默认参数:

for index in range(0, 10):
    action = QtGui.QAction("New", self)
    action.triggered.connect(
        lambda checked, index=index: self.some_function(index))
    self.tabs.append(action)

...

def some_function(self, index):
    action = self.tabs[index]
    print(action.text())
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

传递调用triggered.connect()的QAction对象作为我单击QAction后触发的函数中的参数 的相关文章

随机推荐