Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to add custom delimited completion to QLineEdit using QCompleter ?
Forum Updated to NodeBB v4.3 + New Features

How to add custom delimited completion to QLineEdit using QCompleter ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 3.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dheerajshenoy
    wrote on last edited by
    #1

    Let's say I type Fruits in the QLineEdit and then press space, the QCompleter 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 }.

    JonBJ 1 Reply Last reply
    0
    • D dheerajshenoy

      Let's say I type Fruits in the QLineEdit and then press space, the QCompleter 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 }.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @dheerajshenoy
      I have not played with QCompleter, 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 no MatchNone-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 a QLineEdit 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.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dheerajshenoy
        wrote on last edited by
        #3

        So then I guess I will write my own completer from scratch. Thanks for the input

        JonBJ 1 Reply Last reply
        0
        • D dheerajshenoy

          So then I guess I will write my own completer from scratch. Thanks for the input

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @dheerajshenoy I think so. ISTM QCompleter complations are really oriented at spelling matches.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dheerajshenoy
            wrote on last edited by
            #5

            Okay, now I have got something to work with a custom QWidget and QListWidget. I am showing the QWidget as a popup whenever I need it to show suggestions on a QLineEdit. Only issue now is that the QLineEdit blinking cursor doesn't show up whenever the popup is open. I have changed the FocusPolicy of the popup widget but still no luck. Is there any way to get the cursor back to the QLineEdit ?

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved