将调用 triggered.connect() 的 QAction 对象作为参数传递到我单击 QAction 后触发的函数中
Pass the QAction object which calls triggered.connect() as a parameter in the function that is triggered after i click on QAction
我正在使用 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())
我正在使用 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())