自动完成最后输入的单词
Posted
技术标签:
【中文标题】自动完成最后输入的单词【英文标题】:Autocomplete last typed word 【发布时间】:2021-11-01 12:43:11 【问题描述】:我需要创建一个 GUI,让您输入短语并尝试自动完成输入的每个单词并在其下再次打印输入。当我使用 PyQt5 中的 QCompleter 时,当我输入一个单词时它工作正常,但是一旦我输入第二个单词,我就无法自动完成它,因为它会尝试自动完成整个输入短语。如何为每个输入的单词提供建议?
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QTextEdit, QLineEdit, QCompleter
from PyQt5.QtGui import QStandardItem, QStandardItemModel, QFont
class AppDemo(QWidget):
def __init__(self):
super().__init__()
self.resize(1200, 800)
fnt = QFont('Open Sans', 12)
mainLayout = QVBoxLayout()
# input field
self.input = QLineEdit()
self.input.setFixedHeight(50)
self.input.setFont(fnt)
self.input.textEdited.connect(self.addEntry)
mainLayout.addWidget(self.input)
self.model = QStandardItemModel()
completer = QCompleter(self.model, self)
self.input.setCompleter(completer)
self.console = QTextEdit()
self.console.setFont(fnt)
mainLayout.addWidget(self.console)
self.setLayout(mainLayout)
def addEntry(self):
entryItem = self.input.text()
self.console.clear()
self.console.append(entryItem)
if not self.model.findItems(entryItem):
self.model.appendRow(QStandardItem(entryItem))
app = QApplication(sys.argv)
demo = AppDemo()
demo.show()
sys.exit(app.exec_())
【问题讨论】:
【参考方案1】:既然要添加文本,那么将QCompleter设置为QLineEdit会使任务复杂化,在这种情况下是独立处理QCompleter的逻辑。
from functools import cached_property
from PyQt5.QtGui import QFont, QStandardItem, QStandardItemModel
from PyQt5.QtWidgets import QApplication, QCompleter, QLineEdit
class LineEdit(QLineEdit):
def __init__(self, parent=None):
super().__init__(parent)
self.setFixedHeight(40)
font = QFont("Open Sans", 12)
self.setFont(font)
self.completer.setWidget(self)
self.completer.setModel(self.model)
self.textChanged.connect(self.handle_text_changed)
self.completer.activated.connect(self.handle_activated)
@cached_property
def model(self):
return QStandardItemModel()
@cached_property
def completer(self):
return QCompleter()
def add_word(self, word):
if not self.model.findItems(word):
self.model.appendRow(QStandardItem(word))
def handle_text_changed(self):
text = self.text()[0 : self.cursorPosition()]
if not text:
self.completer.popup().hide()
return
words = text.split()
if text.endswith(" "):
for word in words:
self.add_word(word)
self.completer.popup().hide()
return
self.completer.setCompletionPrefix(words[-1])
self.completer.complete()
def handle_activated(self, text):
prefix = self.completer.completionPrefix()
extra = text[len(prefix) :]
self.blockSignals(True)
self.insert(extra)
self.blockSignals(False)
self.add_word(text)
def main():
import sys
app = QApplication(sys.argv)
lineedit = LineEdit()
lineedit.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()
【讨论】:
以上是关于自动完成最后输入的单词的主要内容,如果未能解决你的问题,请参考以下文章