Search code examples
pythonpython-2.7pyqtpyqt4qlistwidget

python pyQt4 listWidget passing extra arguments


I am having a problem with the .itemClicked() method in PyQt4.

When I use it regularly Ex:

 listObj.itemClicked.connect(some_function)

I can use it in a function

def some_function(self,ITEM):

but I tried pass in another argument with the partial function from functools

listObj.itemClicked.connect( partial(some_function, extra_argument) )

and it replaces the ITEM with the extra_argument.

I also tried using the lambda function

listObj.itemClicked.connect(lambda: some_function(item, extra_argument))

this would work but it would return a tuple that would be stuck on my first item that I clicked.

Ex.

def some_function(self, ITEM, extra_argument):
    str(Item[0].text) # this would return a tuple with the second item 
                      # empty.

I checked around but all I could find was passing extra arguments on a button.

Thank you,

How to pass an extra arguments to PyQt slot?


Solution

  • You should use it as follows:

    extra_argument = {your value}
    listObj.itemClicked.connect(lambda item, extra_argument= extra_argument: self.some_function(item, extra_argument))
    
    def some_function(self, item, extra_argument):
        [...]
    

    Or you can use partial but the default argument should be at the end:

    listObj.itemClicked.connect(partial(self.some_function, extra_argument))
    
    def some_function(self, extra_argument, item):
        [...]