I've been scratching my head over this one. I've got a spell-checking module in my program that produces a list of suggestions for misspelled words. I am trying to add these suggestions to the contextMenuEvent of a custom QTextEdit as QActions. The first way I tried was simply to re-use the same variable name for the action in a for loop:
for i in range(11): #only add the first 10 suggestions if more*
action = QAction(suggestions[i].term) #suggestions[i].term is the string of the suggested word
action.triggered.connect(lambda: self.replace_word(cursor, suggestions[i].term))
menu.addAction(action)
This only added the 10th (suggestions[9].term) suggestion to the context menu, so I figured that the variable, "action," was being overwritten each time. I then decided to us a dictionary to create unique actions:
spell_actions = {}
for i in range(11):
spell_actions['action% s' % str(i)] = QAction(suggestions[i].term)
spell_actions['action% s' % str(i)].triggered.connect(lambda: self.replace_word(cursor, suggestions[i].term))
menu.addAction(spell_actions['action% s' % str(i)])
This resulted in the first 10 suggestions appearing in the context menu. However, when self.replace_word is run, no matter which suggestion was chosen out of the context menu, the word is replaced with the 10th suggestion.
To put it another way, if the context menu has these suggestions:
boat loat load goat moat
No matter which word is chosen from the menu, the word that gets inserted is "moat".
Here's self.replace_word() for reference:
def replace_word(self, cursor, term):
self.setTextCursor(cursor)
self.textCursor().removeSelectedText()
self.textCursor().insertText(term)
Based on the Googling I've done, using a dictionary should have worked, so I've got to be missing something or misunderstanding something.