QAction.triggered通过在上下文菜单中定义动态项触发变量的更改值,并在python中连接pyqt5

时间:2019-11-26 15:25:04

标签: pyqt5 contextmenu

def generateContext(self):
    menu = self.textedit.createStandardContextMenu()
    menu.clear()
    action1 = menu.addAction("Grammar")
    Spell= QMenu('Spell Suggestion')
    menu.insertSeparator(menu.actions()[1])
    menu.insertMenu(menu.actions()[1],Spell)
    Spell.addAction(action1)
    cursor = self.textedit.textCursor()
    cursor.select(QTextCursor.WordUnderCursor)
    self.textedit.setTextCursor(cursor);
    word = cursor.selectedText();
    for words in self.lists():
        #action = Spell.addAction(words)
        action = Spell.addAction(words)
        rec = lambda item =words: self.Correct(item)
        action.setCheckable(True)
        action.triggered[bool].connect(rec)
    menu.exec_(QCursor.pos())

通过for循环在上下文菜单中动态添加项目的列表

def lists(self):
    listSpel=['san francisco','atlanta','london']
    return listSpel

用户单击菜单项时触发的正确方法

def Correct(self,words):
    print(words)
    cursor = self.textedit.textCursor()
    cursor.beginEditBlock()

1 个答案:

答案 0 :(得分:0)

完全不知道您的问题是什么,但这也许会有所帮助:

for words in self.lists():
      action = QAction()
      action.setText(words)
      action.triggered.connect(lambda: self.Correct(words))
      Spell.addAction(action)