Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt QCompleter multiple matches

Tags:

qt

qcompleter

I am trying to make QCompleter match several equivalent options which are separated with commas. There seemingly is no easy way to do that, but one line of QCompleter reference caught my attention, describing function QCompleter::splitPath: "When used with list models, the first item in the returned list is used for matching." Could this be used in the way I need - split the input string and return it so the unfinished last item is the first in the list? I didn't manage to make it work, but I may be doing something wrong.

like image 600
max Avatar asked Sep 23 '10 15:09

max


2 Answers

Here is another way of doing it that I think is more in line with the original question. No need for a complex data model, uses a simple QStringListModel instead.

import sys
from PyQt4 import QtCore, QtGui

class test(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        model = QtGui.QStringListModel()
        wordList = ['John Doe','Jane Doe','Albert Einstein', 'Alfred E Newman']
        model.setStringList(wordList)

        layout = QtGui.QVBoxLayout(self)
        self.line = QtGui.QLineEdit(self)
        layout.addWidget(self.line)

        complete = CustomCompleter(self)
        complete.setModel(model)
        complete.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
        complete.setCompletionMode(0)
        complete.setWrapAround(False)

        self.line.setCompleter(complete)


class CustomCompleter(QtGui.QCompleter):
    def __init__(self, parent=None):
        QtGui.QCompleter.__init__(self, parent)

    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]

#===============================================================================
# Unit Testing
#===============================================================================
if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    window = test()
    window.show()
    sys.exit(app.exec_())
like image 189
Scott Avatar answered Nov 22 '22 08:11

Scott


From what I understand from your question and the doc, you could separate the user-inputted string with commas and make your completer check in your model for a completion.

BUT, after each comma, the index of your model (figure it like a two-dimension array of string) will be incremented.

For example, if you have the following input:

ABCD, EFGH, IJ

and you would like to the completer to finish IJ KL, you would have to have a model that is at least 3 indexes wide because the first text (ABCD) would be completed with the strings in the first column, EFGH would be completed with the second column of your model, etc.

So, I don't know if it could be used in your application.

Best of luck.

like image 26
Live Avatar answered Nov 22 '22 08:11

Live