pythonpyqtpyqt5qlineedit

How to have auto-correct QLineEdit when typing?


How do I have a QLineEdit auto-correct when a key is typed? For this example, when you type the "<" I want it to auto-correct to "less than".

from PyQt5.QtWidgets import *
import sys

class Window(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        layout = QGridLayout()
        self.setLayout(layout)

        # auto complete options                                                 
        names = ["Apple", "Alps", "Berry", "Cherry" ]
        completer = QCompleter(names)

        # create line edit and add auto complete                                
        self.lineedit = QLineEdit()
        self.lineedit.setCompleter(completer)
        layout.addWidget(self.lineedit, 0, 0)


app = QApplication(sys.argv)
screen = Window()
screen.show()
sys.exit(app.exec_())

Solution

  • You can check if a character was added and if so, replace the QLineEdit text. A possible implementation can be override the keyPressEvent method:

    import sys
    
    from PyQt5.QtCore import Qt
    from PyQt5.QtWidgets import QApplication, QCompleter, QGridLayout, QLineEdit, QWidget
    
    
    class LineEdit(QLineEdit):
        def __init__(self, mapping, parent=None):
            super(LineEdit, self).__init__(parent)
    
            self.mapping = mapping
    
        def keyPressEvent(self, event):
            last_text = self.text()
            super(LineEdit, self).keyPressEvent(event)
            new_text = self.text()
            if last_text + event.text() == new_text:
                new_text = self.mapping.get(event.text())
                if new_text is not None:
                    self.setText(last_text + new_text)
    
    
    class Window(QWidget):
        def __init__(self, parent=None):
            super(Window, self).__init__(parent)
            layout = QGridLayout(self)
    
            names = ["Apple", "Alps", "Berry", "Cherry"]
            completer = QCompleter(names)
    
            mapping = {"<": "less than", ">": "greater than", "'": "`"}
            self.lineedit = LineEdit(mapping)
            self.lineedit.setCompleter(completer)
            layout.addWidget(self.lineedit, 0, 0)
    
    
    if __name__ == "__main__":
    
        app = QApplication(sys.argv)
        screen = Window()
        screen.show()
        sys.exit(app.exec_())