PyQt - Auto Completer with QLineEdit multiple times

I want to have the possibility to use multiple times the auto completer in my QLineEdit, I found example using QTextEdit but I can't find for QLineEdit. here is a piece of code I use (very simple one) :

from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
from PyQt5.QtWidgets import *
import sys

def main():    
    app     = QApplication(sys.argv)
    edit    = QLineEdit()
    strList = ["Germany", "Spain", "France", "Norway"]
    completer = QCompleter(strList,edit)

    edit.setCompleter(completer)
    edit.show()

    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

For example, I want the completer to "start predicting" again the words in the same QLineEdit if I add a comma. Thanks.

I've found the answer if it can help others, I created a class for Completer :

class Completer(QtWidgets.QCompleter):

    def __init__(self, parent=None):
        super(Completer, self).__init__(parent)

        self.setCaseSensitivity(Qt.CaseInsensitive)
        self.setCompletionMode(QtWidgets.QCompleter.PopupCompletion)
        self.setWrapAround(False)

    # Add texts instead of replace
    def pathFromIndex(self, index):
        path = QtWidgets.QCompleter.pathFromIndex(self, index)

        lst = str(self.widget().text()).split(',')

        if len(lst) > 1:
            path = '%s, %s' % (','.join(lst[:-1]), path)

        return path

    # Add operator to separate between texts
    def splitPath(self, path):
        path = str(path.split(',')[-1]).lstrip(' ')
        return [path]

And I use it within a class for QLineEdit like :

class TextEdit(QtWidgets.QLineEdit):

    def __init__(self, parent=None):
        super(TextEdit, self).__init__(parent)

        self.setPlaceholderText("example : ")
        self._completer = Completer(self)
        self.setCompleter(self._completer)

pyqt auto complete, I've found the answer if it can help others, I created a class for Completer : class Completer(QtWidgets.QCompleter): def __init__(self,  pyqt auto completion in a table. Viewed 980 times 1. I need auto completion in a table. PyQt - Auto Completer with QLineEdit multiple times.

Solution for PyQt4:

from PyQt4 import QtCore, QtGui

class Completer(QtGui.QCompleter):
    def __init__(self, *args, **kwargs):
        super(Completer, self).__init__(*args, **kwargs)

        self.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
        self.setCompletionMode(QtGui.QCompleter.PopupCompletion)
        self.setWrapAround(False)

    # Add texts instead of replace
    def pathFromIndex(self, index):
        path = QtGui.QCompleter.pathFromIndex(self, index)
        lst = str(self.widget().text()).split(',')
        if len(lst) > 1:
            path = '%s, %s' % (','.join(lst[:-1]), path)
        return path

    def splitPath(self, path):
        path = str(path.split(',')[-1]).lstrip(' ')
        return [path]

class TextEdit(QtGui.QLineEdit):
    def __init__(self, parent=None):
        super(TextEdit, self).__init__(parent)
        words = ["alpha", "omega", "omicron", "zeta"]
        self.setPlaceholderText("example : ")
        self._completer = Completer(words, self)
        self.setCompleter(self._completer)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    w = TextEdit()
    w.show()
    sys.exit(app.exec_())

AutoCompleting QLineEdit · GitHub, pyqt auto complete. PyQt supports autocomplete. If you type in a text box ( QLineEdit ), it can make suggestions. Those suggestions are recommended from a list  Adding auto-completion to a QLineEdit. On the #pyqt IRC channel on Freenode, filip asked how to add auto-completion in a QLineEdit widget.. The following code shows how to use a QStringListModel to supply data to a QCompleter, itself used by a QLineEdit.

here is what i found might be helpful to someone

{

class LineEdit(QLineEdit):
    def __init__(self, *args, **kwargs):
        super(LineEdit,self).__init__( *args, **kwargs)
        self.multipleCompleter = None

    def keyPressEvent(self, event):
        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 = QApplication(sys.argv)
    w = LineEdit()
    completer = QCompleter(["Animals", "Dogs", "Birds", "Cats", "Elephant", "Zebra"])
    completer.setCaseSensitivity(Qt.CaseInsensitive)
    w.setMultipleCompleter(completer)
    w.show()
    sys.exit(app.exec_())

}

PyQt/Adding auto-completion to a QLineEdit, I want to have the possibility to use multiple times the auto completer in my QLineEdit, I found example using QTextEdit but I can't find for QLineEdit. here is a​  The QCompleter class provides completions based on an item model. You can use QCompleter to provide auto completions in any Qt widget, such as QLineEdit and QComboBox. When the user starts typing a word, QCompleter suggests possible ways of completing the word, based on a word list. The word list is provided as a QAbstractItemModel.

QCompleter, I want to have the possibility to use multiple times the auto completer in my QLineEdit, I found example using QTextEdit but I can't find for QLineEdit. here is a​  PySide2.QtWidgets.QLineEdit.setCompleter (completer) ¶ Parameters. completer – QCompleter. Sets this line edit to provide auto completions from the completer, c. The completion mode is set using setCompletionMode(). To use a QCompleter with a QValidator or inputMask, you need to ensure that the model provided to QCompleter contains valid

How to use QCompleter to search more than multiple Values, AutoCompleter.py. from PyQt4.QtCore import QEvent, Qt. from PyQt4.QtGui import QLineEdit self.completions = [] # All available completions. self.partials "%s\n" + \. "Press Tab to autocomplete, multiple times " + \. "to cycle  Python MVC style GUI Temperature Converter. python,user-interface,python-3.x,model-view-controller,tkinter. You have two mistakes here: 1 - In your Counter.py file and in your Convert class methods, you are not return the right variables, instead of return celsius you should return self.celsius and same goes for self.fahrenheit 2 - In Controller.py file: self.view.outputLabel["text"] = self

QCompleter Class Reference, On the #pyqt IRC channel on Freenode, filip asked how to add auto-completion in QtCore import Qt 3 from PyQt4. "__main__": 9 10 app = QApplication(sys.​argv) 11 edit = QLineEdit() 12 completer = QCompleter() 13 edit. QLineEdit object is the most commonly used input field. It provides a box in which one line of text can be entered. In order to enter multi-line text, QTextEdit object is required. The following table lists a few important methods of QLineEdit class − Given below are the most commonly used methods

Comments
  • Hi and welcome to SO maybe you can explain your code a bit. That way people who are not familiar with python can also understand the code.