惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Last Week in AI
Last Week in AI
D
DataBreaches.Net
腾讯CDC
Recent Announcements
Recent Announcements
有赞技术团队
有赞技术团队
A
About on SuperTechFans
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
云风的 BLOG
云风的 BLOG
罗磊的独立博客
月光博客
月光博客
MyScale Blog
MyScale Blog
U
Unit 42
Martin Fowler
Martin Fowler
Stack Overflow Blog
Stack Overflow Blog
T
Tailwind CSS Blog
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
G
Google Developers Blog
博客园 - 【当耐特】
D
Docker
I
InfoQ
雷峰网
雷峰网

WMI

How to Activate MySQL for Multiple Devices - WMI ESLint Flat Config for JS, TS, React, and Prettier - WMI Yarn NPM Registry Configuration - WMI Yarn Guides - WMI Set Up ADB Wireless Debugging on Android: A Full Guide - WMI How to Fill Tax Information on Google Adsense - WMI Migrate ESLint v9 for prettier typescript javascript - WMI PySide6 button click open new window - WMI Mobile Legends To The Stars Event Clue - WMI [PHP] generate random proxy IP:PORT from CIDR - WMI [PHP] generate big text file for testing purpose - WMI List of Chrome Driver command line arguments - WMI Happy eid mubarak - WMI Install markdown engine on vite ESM typescript - WMI Android Activity lifecycle - WMI OkHttp cookie handling on android (webview supported) - WMI Turn git log history into markdown - WMI enable automatic memory heap resizing of android studio - WMI is defining screen density can reduce build time ? - WMI
PySide6 autocomplete input text - WMI
Dimas Lanjaka · 2024-08-17 · via WMI

Sample App

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()

Explanation:

  1. AutocompleteApp Class: This class extends QMainWindow to create the main application window.
  2. __init__ Method: Sets up the window title, size, and central widget.
  3. QLineEdit: A text input field where users type text.
  4. QStringListModel: Holds the list of strings that will be used for autocomplete suggestions.
  5. QCompleter: Provides the autocomplete functionality using the QStringListModel.
  6. Layout: Uses 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.

Helper

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)