[PyQt] Slots with arguments

Peter Bienstman peter.bienstman at gmail.com
Fri Apr 10 12:30:45 BST 2020


I'm building a pop-up menu where the number of entries depends on the
number of plugins a user installs at runtime. The code to deal with
these entries is very similar for all of them, so I want to use a
single slot, but with an argument.

According to some googling, something like this should work when
building the menu:

        self.pronouncer_with_id = {}
        if len(self.pronouncers):
            id_ = 0
            for pronouncer in self.pronouncers:
                id_ += 1
                pronouncer_action = QtWidgets.QAction(\
                    pronouncer.popup_menu_text, popup)
                    lambda: self.pronounce(str(id_)))

My slot looks like this:

    def pronounce(self, id_):

However, all the menu entries result in the argument 'id_'  being the
same, namely the one corresponding to the latest entry added.

Any ideas?



More information about the PyQt mailing list