Closed iMallpa closed 3 months ago
Qcompleter的匹配机制看起来是输入的文本和列表stands中的对比,只有在输入的内容和列表内字符串的文本从左到右匹配才会展示。我觉得这样的匹配机制不妥,我建议只要输入的文本存在于字符串的文本,就展示条目。
Windows 10 22H2
3.12 64bit
5.15.11
may newest
# coding:utf-8 import sys from PyQt5.QtCore import Qt, QStringListModel from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QCompleter from qfluentwidgets import LineEdit, SearchLineEdit class CustomCompleter(QCompleter): """ Custom QCompleter to show all suggestions regardless of input text """ def __init__(self, parent=None): super().__init__(parent) self.setCaseSensitivity(Qt.CaseInsensitive) self.setMaxVisibleItems(10) def splitPath(self, path): # Override this method to always show all completions return self.model().stringList() class Demo(QWidget): def __init__(self): super().__init__() # self.setStyleSheet("Demo {background: rgb(32, 32, 32)}") # setTheme(Theme.DARK) self.hBoxLayout = QHBoxLayout(self) self.lineEdit = SearchLineEdit(self) # add completer stands = [ "威海市", "Hierophant Green", "Made in Haven", "King Crimson", "Silver Chariot", "Crazy diamond", "Metallica", "Another One Bites The Dust", "Heaven's Door", "Killer Queen", "The Grateful Dead", "Stone Free", "The World", "Sticky Fingers", "Ozone Baby", "Love Love Deluxe", "Hermit Purple", "Gold Experience", "King Nothing", "Paper Moon King", "Scary Monster", "Mandom", "20th Century Boy", "Tusk Act 4", "Ball Breaker", "Sex Pistols", "D4C • Love Train", "Born This Way", "SOFT & WET", "Paisley Park", "Wonder of U", "Walking Heart", "Cream Starter", "November Rain", "Smooth Operators", "The Matte Kudasai" ] self.model = QStringListModel(stands) self.completer = CustomCompleter(self.lineEdit) self.completer.setModel(self.model) self.lineEdit.setCompleter(self.completer) self.resize(400, 400) self.hBoxLayout.setAlignment(Qt.AlignCenter) self.hBoxLayout.addWidget(self.lineEdit, 0, Qt.AlignCenter) self.lineEdit.setFixedSize(400, 33) self.lineEdit.setClearButtonEnabled(True) self.lineEdit.setPlaceholderText('搜索地点') if __name__ == '__main__': # enable dpi scale QApplication.setHighDpiScaleFactorRoundingPolicy( Qt.HighDpiScaleFactorRoundingPolicy.PassThrough) QApplication.setAttribute(Qt.AA_EnableHighDpiScaling) QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps) app = QApplication(sys.argv) w = Demo() w.show() app.exec_()
可以告诉我如何修改匹配机制吗
self.completer.setFilterMode(Qt.MatchContains)
谢谢,不过我刚刚通过另一个人做的模糊搜索机制完成了。
What happened?
Qcompleter的匹配机制看起来是输入的文本和列表stands中的对比,只有在输入的内容和列表内字符串的文本从左到右匹配才会展示。我觉得这样的匹配机制不妥,我建议只要输入的文本存在于字符串的文本,就展示条目。
Operation System
Windows 10 22H2
Python Version
3.12 64bit
PyQt/PySide Version
5.15.11
PyQt/PySide-Fluent-Widgets Version
may newest
How to Reproduce?
Minimum code