Solved How do I update a QCompleter with a new strings list?
-
I have the following code which makes a completer with two auto-completes.
test_list.append("APPLE") test_list.append("ORANGE") completer = QCompleter(test_list, self) self.ui.lineText.setCompleter(completer) # CHECKPOINT test_list.append("KIWI")
When I append strings after CHECKPOINT to test_list, the completer is not updated. test_list has KIWI but lineText auto-complete only completes APPLE and ORANGE. It seems I have to update the completer but I don't know how..
What do I do?
-
Hi @user239857
Disclaimer: I'm unfamiliar with Qt for Python... but hopefully this info set you in the right direction.
The problem is that the
QCompleter
takes a copy of your list, and creates a new model (aQStringListModel
in this case) to use, which is no longer associated with yourtest_list
.There's probably better, options, but something like this should work:
test_list.append("APPLE") test_list.append("ORANGE") model = QStringListModel(tes_list); completer = QCompleter(model, self) self.ui.lineText.setCompleter(completer) # CHECKPOINT test_list.append("KIWI") model.setStringList(test_list); # Or: model.setStringList(model.stringList().append("KIWI"));
You could be more efficient, by appending to
model
instead of replacing the list each time, but that's a little more complicated. Ideally you'dbeginInsertRows()
, add the new row, set the (string) data, thenendInsertRows()
. Which would be very efficient, but significantly more code.Cheers.
-
Hi @user239857
Disclaimer: I'm unfamiliar with Qt for Python... but hopefully this info set you in the right direction.
The problem is that the
QCompleter
takes a copy of your list, and creates a new model (aQStringListModel
in this case) to use, which is no longer associated with yourtest_list
.There's probably better, options, but something like this should work:
test_list.append("APPLE") test_list.append("ORANGE") model = QStringListModel(tes_list); completer = QCompleter(model, self) self.ui.lineText.setCompleter(completer) # CHECKPOINT test_list.append("KIWI") model.setStringList(test_list); # Or: model.setStringList(model.stringList().append("KIWI"));
You could be more efficient, by appending to
model
instead of replacing the list each time, but that's a little more complicated. Ideally you'dbeginInsertRows()
, add the new row, set the (string) data, thenendInsertRows()
. Which would be very efficient, but significantly more code.Cheers.
-
@Paul-Colby This was extremely helpful and solved it. Thank you!
-