Код: Выделить всё
def setup_completer(self, input_field):
"""Set up QCompleter for the Diagnostique input field with an empty model initially."""
empty_model = QStringListModel() # Start with an empty model
completer = QCompleter(empty_model)
completer.setCaseSensitivity(Qt.CaseInsensitive)
completer.setFilterMode(Qt.MatchStartsWith)
input_field.setCompleter(completer)
# Connect textChanged signal to handle autocomplete suggestions
input_field.textChanged.connect(
lambda text: self.handle_text_changed(text=text, completer=completer)
)
def handle_text_changed(self, text, completer):
"""Handle text changes and update the popup with suggestions that match the input text."""
# Extract the relevant part of the input text
prefix = text.split("+")[-1].strip()
# Load suggestions from the database
suggestions = self.load_suggestions_from_db()
# Filter matching suggestions
matching_suggestions = [
suggestion for suggestion in suggestions if suggestion.startswith(prefix)
]
# Update the completer's model with matching suggestions
model = completer.model()
if isinstance(model, QStringListModel):
model.setStringList(matching_suggestions)
# Debug prints
print("Prefix extracted:", prefix)
print("Matching suggestions:", matching_suggestions)
Подробнее здесь: https://stackoverflow.com/questions/788 ... r-in-pyqt5