Skip to content
The Chat Archive
sonar LMArena Search Arena 24K

Create a simple text editor with autocomplete using…

  1. User

    Create a simple text editor with autocomplete using QCompleter.CompletionMode.InlineCompletion in python. The suggested completion should just be a random word for now.

  2. Assistant

    Here's how to create a simple text editor with inline autocomplete using PyQt5's QCompleter with InlineCompletion mode. This implementation will suggest random words as completions.

    from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QCompleter
    from PyQt5.QtCore import QStringListModel, Qt
    import random
    import sys
    
    class TextEditor(QMainWindow):
        def __init__(self):
            super().__init__()
            
            # Create text editor widget
            self.text_edit = QTextEdit()
            self.setCentralWidget(self.text_edit)
            
            # Generate some random words for completion
            words = ["apple", "banana", "cherry", "date", "elderberry", 
                    "fig", "grape", "honeydew", "kiwi", "lemon"]
            
            # Create model and completer
            self.completer_model = QStringListModel(words)
            self.completer = QCompleter()
            self.completer.setModel(self.completer_model)
            
            # Set inline completion mode
            self.completer.setCompletionMode(QCompleter.InlineCompletion)
            
            # Set completer to text editor
            self.text_edit.setCompleter(self.completer)
            
            # Window setup
            self.setWindowTitle("Text Editor with Inline Autocomplete")
            self.setGeometry(100, 100, 600, 400)
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        editor = TextEditor()
        editor.show()
        sys.exit(app.exec_())
    

    Key Components Explained:

    1. QTextEdit: This serves as our basic text editor widget where users can type and see inline completions[[3]].

    2. QStringListModel: Holds our list of random words that will be used for suggestions[[7]]. You can replace this with any word list or dynamic content.

    3. QCompleter Configuration:

      • setCompletionMode(QCompleter.InlineCompletion) makes completions appear inline as selected text rather than in a popup[[5]]
      • The model provides the word list for suggestions[[4]]
    4. Implementation Notes:

      • The completer will automatically suggest completions as you type
      • Pressing Tab or Right Arrow will accept the current completion
      • The completion appears as highlighted text directly in the editor[[5]]

    For a more advanced implementation, you could:

    • Dynamically update the word list based on document content
    • Add context-sensitive completions
    • Implement custom completion logic by subclassing QCompleter

    The example uses PyQt5 but the same approach works with PyQt6 with minimal changes to the imports[[4]][[7]]. The InlineCompletion mode provides a clean, unobtrusive autocomplete experience directly in the text.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.