问题
Is there a way to make the QCompleter for pyside work more similar to how the Tag editor here on StackOverflow works? Where a user can type a word and then if there is a space it allows the autocomplete to display matching words?
This post seems like it does what i want but it's in C++ How to force QCompleter to check second word in QLineEdit
import os
import sys
import json
from PySide import QtCore, QtGui
class ExampleWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(ExampleWindow, self).__init__(parent)
self.resize(300, 200)
self.strings_model = QtGui.QStringListModel()
self.get_data(self.strings_model)
completer = QtGui.QCompleter()
completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
completer.setModel(self.strings_model)
self.ui_input = QtGui.QLineEdit()
self.ui_input.setCompleter(completer)
self.ui_input.setPlaceholderText('enter description...')
self.ui_tags_list = QtGui.QListView()
self.ui_tags_list.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
self.ui_tags_list.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
self.ui_tags_list.setModel(self.strings_model)
# main layout
main_layout = QtGui.QVBoxLayout()
main_layout.setContentsMargins(5,5,5,5)
main_layout.setSpacing(5)
main_layout.addWidget(self.ui_input)
main_layout.addWidget(self.ui_tags_list)
main_widget = QtGui.QWidget()
main_widget.setLayout(main_layout)
self.setCentralWidget(main_widget)
# connections
self.ui_input.returnPressed.connect(self.input_entered)
def get_data(self, model):
model.setStringList(["Animals", "Dogs", "Birds", "Cats", "Elephant", "Zebra"])
def append_tag(self, val):
if not val:
return False
if val.lower() in [x.lower() for x in self.strings_model.stringList()]:
return False
self.strings_model.insertRow(self.strings_model.rowCount())
index = self.strings_model.index(self.strings_model.rowCount()-1)
self.strings_model.setData(index, val)
def input_entered(self):
print 'selected word from drop down should be added to lineEdit'
def main():
app = QtGui.QApplication(sys.argv)
ex = ExampleWindow()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
回答1:
You must implement a logic that gets the last string, this will be the word to make the filter in the QComplete
through setCompletionPrefix()
:
class LineEdit(QtGui.QLineEdit):
def __init__(self, *args, **kwargs):
QtGui.QLineEdit.__init__(self, *args, **kwargs)
self.multipleCompleter = None
def keyPressEvent(self, event):
QtGui.QLineEdit.keyPressEvent(self, event)
if not self.multipleCompleter:
return
c = self.multipleCompleter
if self.text() == "":
return
c.setCompletionPrefix(self.cursorWord(self.text()))
if len(c.completionPrefix()) < 1:
c.popup().hide()
return
c.complete()
def cursorWord(self, sentence):
p = sentence.rfind(" ")
if p == -1:
return sentence
return sentence[p + 1:]
def insertCompletion(self, text):
p = self.text().rfind(" ")
if p == -1:
self.setText(text)
else:
self.setText(self.text()[:p+1]+ text)
def setMultipleCompleter(self, completer):
self.multipleCompleter = completer
self.multipleCompleter.setWidget(self)
completer.activated.connect(self.insertCompletion)
def main():
app = QtGui.QApplication(sys.argv)
w = LineEdit()
completer = QtGui.QCompleter(["Animals", "Dogs", "Birds", "Cats", "Elephant", "Zebra"])
completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
w.setMultipleCompleter(completer)
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
来源:https://stackoverflow.com/questions/47832971/qcompleter-supporting-multiple-items-like-stackoverflow-tag-field