I'm porting an application from: python 3.8.5 / PyQt5, to : python 3.10.0 / PySide6
This code adds multiple QAction in a context menu and worked with python 3.8.5 / PyQt5:
for filepath in filepaths:
action = QtGui.QAction(QtGui.QIcon('icon.png'), filepath, self)
action.triggered.connect(lambda checked, path=filepath: self.open_file(path))
menu.addAction(action)
But with python 3.10.0 / PySide6, I get this error when I click on the QAction :
menu.<locals>.<lambda>() missing 1 required positional argument: 'checked'
I'm not sure if it comes from the python upgrade or the PyQt5 => PySide6 change ?
Any workaround to keep a copy of the filepath value in the lambda ?
The problem comes from PySide6 (may be already PySide2) which does some argument analysis and don't permit lambda with default values.
Solution is to use functools.partial :
from functools import partial
class Example:
def fill_menu(self, filepaths):
for filepath in filepaths:
action = QtGui.QAction(QtGui.QIcon('icon.png'), filepath, self)
action.triggered.connect(partial(self.open_file, path))
menu.addAction(action)
def open_file(self, path):
print(path)