Hi, @martin!
Thanks for your quick response.
I seem to have tried almost all the combinations with no success.
E.g.:
# self.OpenRecentList — list of the full filenames/paths
for file in self.OpenRecentList:
new_act = QAction(file, self)
self.menuOpen_Recent.addAction(new_act)
new_act.triggered.connect(lambda filename=file: self.OpenRecent(filename))
def OpenRecent(self, file):
# Code that opens a file
Result: When triggering the created actions(menu items) it tries to pass the boolean instead of the filename, because the error “File False not found” is raised within my app. It seems to be the boolean from the ‘checked’ parameter which is sent from the triggered signal, but omitted in my code.
—
for file in self.OpenRecentList:
def open_recent_connector(filename=file):
return self.OpenRecent(filename)
new_act = QAction(file, self)
self.menuOpen_Recent.addAction(new_act)
new_act.triggered.connect(open_recent_connector)
Result: the same as above.
for file in self.OpenRecentList:
def open_recent_connector(checked, filename=file):
return self.OpenRecent(filename)
new_act = QAction(file, self)
self.menuOpen_Recent.addAction(new_act)
new_act.triggered.connect(open_recent_connector)
Result (on triggering the action): TypeError: open_recent_connector() missing 1 required positional argument: ‘checked’.
for file in self.OpenRecentList:
filename = file
def open_recent_connector():
return self.OpenRecent(filename)
new_act = QAction(file, self)
self.menuOpen_Recent.addAction(new_act)
new_act.triggered.connect(open_recent_connector)
Result (on triggering the action): the OpenRecent function is called (it opens the file), but the ‘loop problem’, described in your book, occurs. It passes the value of the last iteration when any QAction is triggered, and my app always opens the last file in the list.
Trying to fix it using your ‘naming method’ I went even as far as that:
new_act = []
for ind, file in enumerate(self.OpenRecentList):
def open_recent_connector():
filename = self.OpenRecentList[ind]
return self.OpenRecent(filename)
new_act.append(QAction(file, self))
self.menuOpen_Recent.addAction(new_act[ind])
new_act[ind].triggered.connect(open_recent_connector)
Result (on triggering the action): the same as above (the loop problem).