Results 1 to 3 of 3

Thread: QTextEdit and QCompleter

  1. #1
    Join Date
    Jun 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTextEdit and QCompleter

    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!

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTextEdit and QCompleter

    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.)

    HTH

  3. #3
    Join Date
    Jun 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit and QCompleter

    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?

Similar Threads

  1. QTextEdit with QCompleter
    By No-Nonsense in forum Qt Programming
    Replies: 1
    Last Post: 20th February 2007, 14:48
  2. QCompleter with QTextEdit
    By ePharaoh in forum Qt Programming
    Replies: 1
    Last Post: 19th July 2006, 13:03

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.