I'm having this weird problem, I'm creating file menu action items from a list that I have. It displays properly but behaves the wrong way.
Here's an example snippet of my code:
self.dMenu = self.menuBar().addMenu('&Fruit')
for entry in ['apple', 'banana', 'orange', 'strawberry']:
item = QAction(QIcon(), entry, self)
item.triggered.connect(lambda: print(item.text())) #Prints the menu action's text when clicked
self.dMenu.addAction(item)
In the file menu it shows up as:
- apple
- banana
- orange
- strawberry
When I click on the item it should display the action item's text but it always prints strawberry
regardless of which I press. (Even though it shows the text as apple
for example.)
I also tried creating an array of QAction menu items to see if that would solve the problem.
self.dMenu = self.menuBar().addMenu('&Fruit')
actionItems = {}
for entry in ['apple', 'banana', 'orange', 'strawberry']:
actionItems[entry] = QAction(QIcon(), entry, self)
actionItems[entry].triggered.connect(lambda: print(actionItems[entry].text())) #Prints the menu action's text when clicked
self.dMenu.addAction(actionItems[entry])
Same results though. I'm not really sure what's causing all the action items to behave the same even though each is being created as it's own instance. I initially set the action to just print entry
but that didn't do anything, I was hoping printing the instance's text would fix the problem but it seems like that didn't work either.
Edit: Just want to mention the array I'm using is much larger and is changing frequently so I can't just hard code each entry in the menu.