text edit completer

  • i'm looking at this example as a reference for me to write a completer for QPlainTextEdit subclass:

    however, it only shows autocompletion on some shortcut press. what i wanna do is, it to behave like a normal auto completion, i.e. filter suggestion list based on what already typed.

    i can think of updating the model with every key press but i think that's a heavy operation. any better solution?

  • Lifetime Qt Champion

    That is the normal operation. (guess on type)

    QStringList wordList;
      wordList << "alpha" << "omega" << "omicron" << "zeta";
      QCompleter *completer = new QCompleter(wordList, this);

    Guess as soon as you write. However for a TextEdit that might be annoying and hence i thinks that's why sample uses shortcut.

  • @mrjj
    okay, but in my case i need it.

  • Lifetime Qt Champion

    You are free to call comple() at any moment. so its more up to you to trigger it.
    maybe on keypress will do it.

  • @mrjj
    i call it. i do it as in example. but in my case there are spaces in between the words, and i was selecting by text_cursor.select(QTextCursor::WordUnderCursor). changed that to text_cursor.select(QTextCursor::LineUnderCursor), now works!

  • there's also something else: as in the example, i applied setModelSorting(QCompleter::CaseSensitivelySortedModel) to the completer. however, it wasn't sorted, and i had to do completer->model()->sort(0) explicitly. i don't think i should've done that, right?

    i mean, just setModelSorting() should've done the job:
    from docs: This property holds the way the model is sorted

  • Lifetime Qt Champion

    @user4592357 said in text edit completer:


    As far as i know its only for hinting the sort order. setting it wont
    tell model to sort. Its more like a clue for completer.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.