= Adding tab-completion to a QLineEdit = On the `#pyqt` IRC channel on [[http://www.freenode.net|Freenode]], `sonic` asked how to handle key events for a QLineEdit widget to enable tab-completion. The following code shows how to subclass QLineEdit and reimplement the `keyPressEvent()` method. The tab-completion code isn't ideal - you should probably consider using a QCompleter object to handle the process of completing an incomplete piece of text. {{{ #!python import sys from PyQt4.QtCore import Qt from PyQt4.QtGui import QApplication, QLineEdit class LineEdit(QLineEdit): completion_items = [ u"hello", u"world" ] def __init__(self, parent = None): QLineEdit.__init__(self, parent) def keyPressEvent(self, event): if event.key() == Qt.Key_Tab: for item in self.completion_items: if item.startswith(self.text()): self.setText(item) break event.accept() else: QLineEdit.keyPressEvent(self, event) if __name__ == "__main__": app = QApplication(sys.argv) window = LineEdit() window.show() sys.exit(app.exec_()) }}}