如何在 PyQt5 中更新 QCompleter 的自动完成建议
How to update auto completion suggestions of QCompleter in PyQt5
我正在向 QTextEdit 添加自动完成器,并且在向 AutoCompleter class 中的 auto_completion_list
附加建议后尝试刷新 QCompleter class,然后我需要刷新或更新 QCompleter 的建议。怎么做?
这是我的代码:
class AutoCompleter(QCompleter):
insertText = pyqtSignal(str)
def __init__(self, parent=None):
self.auto_completion_list = ['Suggestion1','Suggestion2','Suggestion3']
super().__init__(self.auto_completion_list, parent)
self._parent = parent
self.setCompletionMode(QCompleter.PopupCompletion)
self.popup().setStyleSheet(qdarkstyle.load_stylesheet())
self.popup().setFixedSize(QSize(125,70))
self.popup().setFont(QFont("Consolas",11))
self.highlighted.connect(self.setHighlighted)
def add(self,suggestion):
#What Should I do here After appending keyword to refresh QCompleter?
self.auto_completion_list.append(keyword)
def setHighlighted(self, text):
self.lastSelected = text
def getSelected(self):
return self.lastSelected
class Editor(QTextEdit):
def __init__(self,parent=None):
super().__init__(parent)
self.completer = AutoCompleter()
self.completer.setWidget(self)
self.completer.insertText.connect(self.insertCompletion)
def insertCompletion(self, completion):
tc = self.textCursor()
extra = (len(completion) - len(self.completer.completionPrefix()))
tc.movePosition(QTextCursor.Left)
tc.movePosition(QTextCursor.EndOfWord)
tc.insertText(completion[-extra:])
self.setTextCursor(tc)
self.completer.popup().hide()
def focusInEvent(self, event):
if self.completer:
self.completer.setWidget(self)
QTextEdit.focusInEvent(self, event)
def keyPressEvent(self,event):
tc = self.textCursor()
if event.key() == Qt.Key_Tab:
if self.completer.popup().isVisible():
self.completer.insertText.emit(self.completer.getSelected())
self.completer.setCompletionMode(QCompleter.PopupCompletion)
else:
tc = self.textCursor()
tc.insertText(" ")
return
QTextEdit.keyPressEvent(self, event)
tc.select(QTextCursor.WordUnderCursor)
cr = self.cursorRect()
if len(tc.selectedText()) > 0:
self.completer.setCompletionPrefix(tc.selectedText())
popup = self.completer.popup()
popup.setCurrentIndex(self.completer.completionModel().index(0,0))
cr.setWidth(self.completer.popup().sizeHintForColumn(0)
+ self.completer.popup().verticalScrollBar().sizeHint().width())
self.completer.complete(cr)
else:
self.completer.popup().hide()
QCompleter 使用一个模型,当用字符串列表初始化时,它就是一个 QStringListModel。您可以使用 setStringList()
:
更新列表
class AutoCompleter(QCompleter):
# ...
def add(self, suggestion):
self.auto_completion_list.append(suggestion)
self.model().setStringList(self.auto_completion_list)
我正在向 QTextEdit 添加自动完成器,并且在向 AutoCompleter class 中的 auto_completion_list
附加建议后尝试刷新 QCompleter class,然后我需要刷新或更新 QCompleter 的建议。怎么做?
这是我的代码:
class AutoCompleter(QCompleter):
insertText = pyqtSignal(str)
def __init__(self, parent=None):
self.auto_completion_list = ['Suggestion1','Suggestion2','Suggestion3']
super().__init__(self.auto_completion_list, parent)
self._parent = parent
self.setCompletionMode(QCompleter.PopupCompletion)
self.popup().setStyleSheet(qdarkstyle.load_stylesheet())
self.popup().setFixedSize(QSize(125,70))
self.popup().setFont(QFont("Consolas",11))
self.highlighted.connect(self.setHighlighted)
def add(self,suggestion):
#What Should I do here After appending keyword to refresh QCompleter?
self.auto_completion_list.append(keyword)
def setHighlighted(self, text):
self.lastSelected = text
def getSelected(self):
return self.lastSelected
class Editor(QTextEdit):
def __init__(self,parent=None):
super().__init__(parent)
self.completer = AutoCompleter()
self.completer.setWidget(self)
self.completer.insertText.connect(self.insertCompletion)
def insertCompletion(self, completion):
tc = self.textCursor()
extra = (len(completion) - len(self.completer.completionPrefix()))
tc.movePosition(QTextCursor.Left)
tc.movePosition(QTextCursor.EndOfWord)
tc.insertText(completion[-extra:])
self.setTextCursor(tc)
self.completer.popup().hide()
def focusInEvent(self, event):
if self.completer:
self.completer.setWidget(self)
QTextEdit.focusInEvent(self, event)
def keyPressEvent(self,event):
tc = self.textCursor()
if event.key() == Qt.Key_Tab:
if self.completer.popup().isVisible():
self.completer.insertText.emit(self.completer.getSelected())
self.completer.setCompletionMode(QCompleter.PopupCompletion)
else:
tc = self.textCursor()
tc.insertText(" ")
return
QTextEdit.keyPressEvent(self, event)
tc.select(QTextCursor.WordUnderCursor)
cr = self.cursorRect()
if len(tc.selectedText()) > 0:
self.completer.setCompletionPrefix(tc.selectedText())
popup = self.completer.popup()
popup.setCurrentIndex(self.completer.completionModel().index(0,0))
cr.setWidth(self.completer.popup().sizeHintForColumn(0)
+ self.completer.popup().verticalScrollBar().sizeHint().width())
self.completer.complete(cr)
else:
self.completer.popup().hide()
QCompleter 使用一个模型,当用字符串列表初始化时,它就是一个 QStringListModel。您可以使用 setStringList()
:
class AutoCompleter(QCompleter):
# ...
def add(self, suggestion):
self.auto_completion_list.append(suggestion)
self.model().setStringList(self.auto_completion_list)