





























sample PySide6 application with autocomplete functionality using QLineEdit and QCompleter. This example creates a simple window with a text input field that provides autocomplete suggestions as the user types.
from PySide6.QtWidgets import QApplication, QMainWindow, QLineEdit, QCompleter, QVBoxLayout, QWidget
from PySide6.QtCore import QStringListModel
class AutocompleteApp(QMainWindow):
def __init__(self):
super().__init__()
# Set up the main window
self.setWindowTitle("Autocomplete Example")
self.setGeometry(100, 100, 400, 200)
# Create a central widget and a layout
central_widget = QWidget()
layout = QVBoxLayout()
# Create QLineEdit widget
self.line_edit = QLineEdit()
self.line_edit.setPlaceholderText("Type something...")
# Create sample data for autocomplete
data = ["apple", "banana", "grape", "orange", "peach", "pear", "plum"]
# Create QStringListModel and QCompleter
self.model = QStringListModel(data)
self.completer = QCompleter(self.model)
self.line_edit.setCompleter(self.completer)
# Add the QLineEdit to the layout
layout.addWidget(self.line_edit)
central_widget.setLayout(layout)
self.setCentralWidget(central_widget)
if __name__ == "__main__":
app = QApplication([])
window = AutocompleteApp()
window.show()
app.exec()
AutocompleteApp Class: This class extends QMainWindow to create the main application window.__init__ Method: Sets up the window title, size, and central widget.QLineEdit: A text input field where users type text.QStringListModel: Holds the list of strings that will be used for autocomplete suggestions.QCompleter: Provides the autocomplete functionality using the QStringListModel.QVBoxLayout to arrange the QLineEdit within the main window.To run this application, save the code to a file (e.g., autocomplete_app.py) and execute it with Python. Make sure you have PySide6 installed in your environment.
PySide6 set QLineEdit autocomplete helper
from PySide6.QtWidgets import QLineEdit, QCompleter
from PySide6.QtCore import QStringListModel
from typing import List
def set_completer(line_edit: QLineEdit, data: List[str]) -> None:
"""
Configures a QLineEdit widget with a QCompleter for autocomplete functionality.
Args:
line_edit (QLineEdit): The QLineEdit widget to which the completer will be applied.
data (List[str]): A list of strings to be used as autocomplete suggestions.
Returns:
None
"""
# Create a QStringListModel with the provided data
model = QStringListModel(data)
# Create a QCompleter with the model
completer = QCompleter(model)
# Set the completer on the QLineEdit
line_edit.setCompleter(completer)此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。