Sahil Gupta Sahil Gupta - 11 months ago 43
Python Question

Pass the QAction object which calls triggered.connect() as a parameter in the function that is triggered after i click on QAction

i am creating list of QAction objects using for loop like this:

class some_class:
self.tabs = []

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

def some_function(self):
print self.i

whenever i click on any of the tabs created, it triggers only tabs[9] and print "9" only.

So how to pass the QAction object itself in the some_function which triggered some_function()


Cache the index as a default argument:

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


def some_function(self, index):
    action = self.tabs[index]