0

I'm attemping to setup a function that will create QActions and connect them with methods. The methods that I pass vary in terms of arguments that they receive. Is there a way to pass all individual arguments from a tuple (args) into a function regardless of the quantity? I already have most of my code written, so I don't want to change all of my methods at this point.

In the absence of a solution, I have the below:

def action_gen(self, menu, text, method, *args, passing=0):
    action= QAction(text)
    menu.addAction(action)
    if passing == 0:
        base.triggered.connect(method)
    elif passing == 1:
       base.triggered.connect(lambda: method(args[0]))
    elif passing == 2:
       base.triggered.connect(lambda: method(args[0], args[1]))
    self.all_actions[text] = base
    return```
  • As long as `passing` is only intended to reflect the actual number of arguments passed to `action_gen`, just use `lambda: method(*args)` as the lone case and don't bother adding `passing` as a parameter. – chepner Jul 18 '22 at 19:48
  • I could have sworn that I tested that and it didn't work. But now it does. Thanks a million! – sethdhanson Jul 18 '22 at 19:57

0 Answers0