Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Is there a QCompleter "initiated" signal?



  • Qt5. Using a QCompleter(QStringList), with QCompleter.setCompletionMode(QCompleter.PopupCompletion). Attached to a QLineEdit.

    My string list is "expensive" to populate. So while I attach the completer to the line edit early, I'd like to delay population of the list till the very last moment, which I take to be when the user types a character into the line edit or whatever and the first attempt at a completion is made? So I'd like something like:

    # assign empty list initially
    completer = QCompleter([])
    # connect the "initiated" signal to populate the items at that point
    completer.firsttimeattemptcompletesignal.connect(lambda: completer.getNewStringList())
    

    I see two problems; they may be inter-related:

    • First & foremost, what can I use for completer.firsttimeattemptcompletesignal? I see no signal from the QCompleter? (Also bear in mind that at this point the possible completions will be empty, so I'd need a signal that would still fire even if no completions are available.) Do I have to do something on the QLineEdit instead (yuck)?

    • Second, I see from QCompleter(QStringList) that QStringList items is only available during constructor, cannot be added to later, right? So to achieve change of items I'd have to change over to model? (And I'm not sure whether there aren't issues with "dynamic repopulation of model during completion" in any case?)

    If it all gets too tricky, should I perhaps not create the QCompleter at all initially, then look at the QLineEdit for something like click/character typed, and only at that instant create the QCompleter populated with strings read at that point, attach it to line edit, and then do what to get to how it would be if the completer had been attached before the click/key stroke?


  • Lifetime Qt Champion

    Hi,

    You can also give it a QAbstractItemModel so you could create a small QAbstractListModel subclass and when data is called check whether you already loaded your list and do it at that time.

    On a parallel idea, you could copy QFileSystemModel and load your data in a separated thread.



  • So it seems there isn't any notification from QCompleter that it needs the completion list now, so go get it, which is a shame. (Think of comboboxes: it's not uncommon to have them populated "on demand", so you get a notification as the user clicks the "expand list arrow" to do the population; QCompleter could do with that, the list is liable to be even longer/more expensive than what you'd store in a combobox.)

    @SGaist
    Yours is probably the best way to go, but I'm losing the will to live on this.

    when data is called check whether you already loaded your list

    I'd need a definitive list for which functions then, to be certain I populate in time regardless of what user does. I suspect just data() will be too late, won't rowCount() likely get called earlier? Who knows. So given that Qt docs don't give me that explicitly, I've given up, collapsed in a heap, and pre-populated the QCompleter after all. If users moan it's a delay, I'll say it's a Qt limitation... :)


  • Lifetime Qt Champion

    You can use an event filter on the view used by the QComboBox and react on its show event.



  • @SGaist
    But this is a QCompleter, and it can have various CompletionModes, not sure how they would work with events? Also I'm thinking any show event might come after a call wanting the possible completions before it tries to show them, and that would be too late.


Log in to reply