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

Sah*_*pta 5 python signals-slots pyqt4 python-2.7

我正在使用 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
Run Code Online (Sandbox Code Playgroud)

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

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

ekh*_*oro 8

将索引缓存为默认参数:

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())
Run Code Online (Sandbox Code Playgroud)