How to add custom delimited completion to QLineEdit using QCompleter ?
-
Let's say I type Fruits in the
QLineEdit
and then press space, theQCompleter
completion should give me some suggestion like list of fruits{ Apple, Orange }
. If I remove the first word and type in let's say Color and then press space it should now suggest a list of colors{ Red, Blue }
. -
@dheerajshenoy
I have not played withQCompleter
, so take this with a pinch of salt. I am not saying it cannot be done, but I think it may be "difficult".Obviously you would have to provide a custom model to deal with recognising
Fruit
and (only after typing space) returning{ Apple, Orange }
. But I think you will fall over because of the mandatory filterMode : Qt::MatchFlags property:This property controls how filtering is performed.
If filterMode is set to Qt::MatchStartsWith, only those entries that start with the typed characters will be displayed. Qt::MatchContains will display the entries that contain the typed characters, and Qt::MatchEndsWith the ones that end with the typed characters.
Setting filterMode to any other Qt::MatchFlag will issue a warning, and no action will be performed.
You cannot use any of the
Match...
flags for your desired completion list (and there is noMatchNone
-type flag). Which means at best you will (apparently) get a warning.Trying to use
QCompleter
for your purpose is like trying to put a square peg in a round hole. It is designed around the spelling of the completion items matching the "prefix" which has been typed, and yours does not work this way at all.I would look at writing your own "completer" code rather than trying to squeeze it out of
QCompleter
. I'm not at all sure about aQLineEdit
in the first place: unless you are writing a real-world generative AI the number of words the user can type which have such a list of completions must be severely limited to whatever you have pre-anticipated, a small number, so maybe the user should pick from those in some list? Depends what you want to achieve, and what you want to do with any words which are not in the list. -
So then I guess I will write my own completer from scratch. Thanks for the input
-
@dheerajshenoy I think so. ISTM
QCompleter
complations are really oriented at spelling matches. -
Okay, now I have got something to work with a custom
QWidget
andQListWidget
. I am showing theQWidget
as a popup whenever I need it to show suggestions on aQLineEdit
. Only issue now is that theQLineEdit
blinking cursor doesn't show up whenever the popup is open. I have changed theFocusPolicy
of the popup widget but still no luck. Is there any way to get the cursor back to theQLineEdit
?