View Full Version : QTextEdit and QCompleter

12th June 2009, 23:15
Hello everyone,

I am writing a simple text editor based on QTextEdit with autocompletion functionality for which I am using QCompleter. What I want to achieve is that the list of suggestions will be created based on the previous word entered by the user (i.e. due to grammar or syntax). The problem that I just can't overcome is how to obtain that "previous word"? I just want to be somehow able to access the word before the prefix being entered and before the space separating the two words. Can anyone provide any hint for achieving this? Thanks for any help!

13th June 2009, 08:57
see QTextEdit::textCursor(), and QTextCursor

(Basically, obtain a cusor for the current position, then move it back to the start of your word, skip the whitespace, and the move it (selecting text by using MoveMode QTextCursor::KeepAnchor) over the previous word. Finally grab the text by QTextCursor::selectedText().
Iirc, using the cursor obtained by QTextEdit::textCursor() would really change the text edit's selection, so you will probably need to work on a copy of the QTextCursor.)


14th June 2009, 04:18
Thanks for your reply!
I looked at the documentation for QTextEdit::textCursor() and it seems that it already returns a copy of a current cursor. If I understand correctly then, after I move the cursor back, select the previous word and store it as a QString I can just delete the obtained cursor and work on the string?