Dynamic QCompleter for QLineEdit
-
I'm trying to get a QCompleter working for a QLineEdit. The completer's model is dynamically filled with values from a network request that's made with the line edit's text, but with the delayed update, the completer's popup is hidden and doesn't show up unless the line edit's text matches something from what was in the model previously.
Calling the completer's complete method manually works, but causes flicker because the completer hides its popup on key events.
Is there any way of getting this to work nicely, or will I have to reimplement some of the completer's methods to get it working?from PySide6 import QtCore, QtWidgets from __feature__ import snake_case, true_property # noqa: F401 class Window(QtWidgets.QMainWindow): def __init__(self): super().__init__() self.main_widget = QtWidgets.QWidget(self) self.set_central_widget(self.main_widget) self.layout_ = QtWidgets.QVBoxLayout(self.main_widget) self.line_edit = QtWidgets.QLineEdit(self) self.layout_.add_widget(self.line_edit) self.completer_model = QtCore.QStringListModel(self) self.completer = QtWidgets.QCompleter(self.completer_model, self) self.line_edit.textEdited.connect(self.update_model) self.line_edit.set_completer(self.completer) def update_model(self, query: str): """Simulate the network requests that calls self.update_callback when finished.""" QtCore.QTimer.single_shot(0, lambda: self.update_callback(query)) #self.completer_model.set_string_list([query + "completedtext"]) def update_callback(self, query): self.completer_model.set_string_list([query + "completedtext"]) app = QtWidgets.QApplication() window = Window() window.show() app.exec() -
Hi,
Did you consider creating a custom model to handle the data ? Or do you have a particular reason to replace all the entries every time ?
-
Hence my suggestion of a custom model.
-