[PyQt] QCompleter in a QLineEdit

C. B. Esquire crackerbunny at gmail.com
Wed Apr 27 07:29:48 BST 2011


(let me know if the space/tab formatting didn't come through and I'll attach
the python script instead)

I'm trying to create a QCompleter, which works fine if I just build the
QLineEdit.
However if I drop the QLineEdit into a QMainWindow, the QCompleter no longer
works. What am I doing wrong here?


*# LineEdit class*
import sys
from PyQt4 import QtCore, QtGui

class LineEdit(QtGui.QLineEdit):
    def __init__(self, parent=None):
        super(LineEdit, self).__init__(parent)

        self.setFocusPolicy(QtCore.Qt.StrongFocus)

        self.completer = QtGui.QCompleter(self)

self.completer.setCompletionMode(QtGui.QCompleter.UnfilteredPopupCompletion)
        self.pFilterModel = QtGui.QSortFilterProxyModel(self)

self.pFilterModel.setFilterCaseSensitivity(QtCore.Qt.CaseInsensitive)
        self.completer.setPopup(self.view())
        self.setCompleter(self.completer)

self.textEdited[unicode].connect(self.pFilterModel.setFilterFixedString)

        self.setModelColumn(0)

    def setModel(self, model):
        self.pFilterModel.setSourceModel(model)
        self.completer.setModel(self.pFilterModel)

    def setModelColumn( self, column ):
        self.completer.setCompletionColumn(column)
        self.pFilterModel.setFilterKeyColumn(column)

    def view(self):
        return self.completer.popup()

    def index( self ):
        return self.currentIndex()

*# Instance 1: This works just fine*
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)

    model = QtGui.QStandardItemModel()

    for i,word in enumerate(['test', 'blah', 'heh', 'yep']):
        item = QtGui.QStandardItem(word)
        model.setItem(i, 0, item)

    lineEdit = LineEdit()
    lineEdit.setModel(model)
    lineEdit.setModelColumn(0)
    lineEdit.show()
    sys.exit(app.exec_())
*
**# Instance 2: Building** the LineEdit inside a QMainWindow the QCompleter
doesn't work any more ~*
if __name__ == '__main__':
    class Example(QtGui.QMainWindow):
        def __init__(self):
            QtGui.QMainWindow.__init__(self)

            self.centralWidget = QtGui.QWidget(self)
            self.layout = QtGui.QVBoxLayout(self.centralWidget)

            # can I push this inside the LineEdit class instead?
            model = QtGui.QStandardItemModel()
            for i, word in enumerate(['test', 'blah', 'heh', 'yep', 'hello',
'hi']):
                item = QtGui.QStandardItem(word)
                model.setItem(i, 0, item)

            # Make a LineEdit instance
            self.lineEdit = LineEdit(parent=self.centralWidget)
            self.lineEdit.setModel(model)

            self.layout.addWidget(self.lineEdit)
            self.setCentralWidget(self.centralWidget)


    app = QtGui.QApplication(sys.argv)
    QtWin = Example()
    QtWin.show()
    sys.exit(app.exec_())






*
### Also a side question, is there a way to put the model code inside the
LineEdit class? When I put the model logic inside, it also seems to quietly
stop working.*

            model = QtGui.QStandardItemModel()
            for i, word in enumerate(['test', 'blah', 'heh', 'yep', 'hello',
'hi']):
                item = QtGui.QStandardItem(word)
                model.setItem(i, 0, item)

            # Make a LineEdit instance
            self.lineEdit = LineEdit(parent=self.centralWidget)
            self.lineEdit.setModel(model)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20110427/5ba82b9f/attachment.html>


More information about the PyQt mailing list