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()