(let me know if the space/tab formatting didn't come through and I'll attach the python script instead)<br><br>I'm trying to create a QCompleter, which works fine if I just build the QLineEdit. <br>However if I drop the QLineEdit into a QMainWindow, the QCompleter no longer works. What am I doing wrong here?<br>
<br><br><b># LineEdit class</b><br>import sys<br>from PyQt4 import QtCore, QtGui<br><br>class LineEdit(QtGui.QLineEdit):<br> def __init__(self, parent=None):<br> super(LineEdit, self).__init__(parent)<br> <br>
self.setFocusPolicy(QtCore.Qt.StrongFocus)<br> <br> self.completer = QtGui.QCompleter(self)<br> self.completer.setCompletionMode(QtGui.QCompleter.UnfilteredPopupCompletion)<br> self.pFilterModel = QtGui.QSortFilterProxyModel(self)<br>
self.pFilterModel.setFilterCaseSensitivity(QtCore.Qt.CaseInsensitive)<br> self.completer.setPopup(self.view())<br> self.setCompleter(self.completer)<br> self.textEdited[unicode].connect(self.pFilterModel.setFilterFixedString)<br>
<br> self.setModelColumn(0)<br> <br> def setModel(self, model):<br> self.pFilterModel.setSourceModel(model)<br> self.completer.setModel(self.pFilterModel)<br> <br> def setModelColumn( self, column ):<br>
self.completer.setCompletionColumn(column)<br> self.pFilterModel.setFilterKeyColumn(column)<br> <br> def view(self):<br> return self.completer.popup()<br> <br> def index( self ):<br>
return self.currentIndex()<br> <br><b># Instance 1: This works just fine</b><br>if __name__ == "__main__":<br> app = QtGui.QApplication(sys.argv)<br> <br> model = QtGui.QStandardItemModel()<br>
<br> for i,word in enumerate(['test', 'blah', 'heh', 'yep']):<br> item = QtGui.QStandardItem(word)<br> model.setItem(i, 0, item)<br> <br> lineEdit = LineEdit()<br>
lineEdit.setModel(model)<br> lineEdit.setModelColumn(0)<br> lineEdit.show()<br> sys.exit(app.exec_())<br><b><br></b><b># Instance 2: Building</b><b> the LineEdit inside a QMainWindow the QCompleter doesn't work any more ~</b><br>
if __name__ == '__main__':<br> class Example(QtGui.QMainWindow):<br> def __init__(self):<br> QtGui.QMainWindow.__init__(self)<br> <br> self.centralWidget = QtGui.QWidget(self)<br>
self.layout = QtGui.QVBoxLayout(self.centralWidget)<br> <br> # can I push this inside the LineEdit class instead?<br> model = QtGui.QStandardItemModel()<br> for i, word in enumerate(['test', 'blah', 'heh', 'yep', 'hello', 'hi']):<br>
item = QtGui.QStandardItem(word)<br> model.setItem(i, 0, item)<br> <br> # Make a LineEdit instance<br> self.lineEdit = LineEdit(parent=self.centralWidget)<br>
self.lineEdit.setModel(model)<br> <br> self.layout.addWidget(self.lineEdit)<br> self.setCentralWidget(self.centralWidget)<br> <br> <br> app = QtGui.QApplication(sys.argv)<br>
QtWin = Example()<br> QtWin.show()<br> sys.exit(app.exec_())<br><br><br><br><br><br><br><i><b><br>### 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.</b></i><br>
<br> model = QtGui.QStandardItemModel()<br> for i, word in enumerate(['test', 'blah', 'heh', 'yep', 'hello', 'hi']):<br> item = QtGui.QStandardItem(word)<br>
model.setItem(i, 0, item)<br> <br> # Make a LineEdit instance<br> self.lineEdit = LineEdit(parent=self.centralWidget)<br> self.lineEdit.setModel(model)<br>