2

I'm using QCompleter in my QGIS plugin. The code below works well, but I'd love to make it more sophisticated. In this example I have some strings that are composed of some words, for example "How to use QCompleter". In this moment, to see the completer I need to start writing by the first word ("How"). I'm wondering if there's a way to see the completer also writing in the LineEdit any word in the string, for example "to" or "use".

strList = ["Prove PyQt5", "How to use Qcompleter", "I love StackExchange"] 
completer = QCompleter()
completer.setCaseSensitivity(0)
dlg.lineEdit.setCompleter(completer)
model = QStringListModel()
model.setStringList(strList)
completer.setModel(model) 
  • P.s. I know that the question is a bit off-topic, but I also know that here there are some people that are able to solve this problem. – Lorenzo Jan 29 at 18:49
2

You can use Qt.MatchContains flag as filter mode.

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QCompleter

completer = QCompleter()
completer.setFilterMode(Qt.MatchContains)

For further information: Filter Modes for QCompleter.

| improve this answer | |
  • Hi, many thanks once again. I always have trouble finding the right documentation for Qt. – Lorenzo Jan 29 at 19:12
  • I editted the link. In previous link, just three of them are implemented for QCompleter. – Kadir Şahbaz Jan 29 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.