Results 1 to 3 of 3

Thread: QTextCursor::WordUnderCursor

  1. #1
    Join Date
    May 2007
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default QTextCursor::WordUnderCursor

    Hi,

    I've got some weird problems with the QTextCursor::WordUnderCursor method.

    Suposed I'm on a line with the following text : "link,"

    then QTextCursor::WordUnderCursor returns me "link" only.

    I supose it doesn't take into account some chars like opening braces, comas, etc. by default, but how can I get those chars too ?

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextCursor::WordUnderCursor

    You can fetch the position of the cursor and then access the text block at that position and process the block the way you want.

  3. #3
    Join Date
    May 2007
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextCursor::WordUnderCursor

    Thanks,

    I solved it like this (Python code, sorry) :

    Qt Code:
    1. oldpos = my_cursor.position()
    2.  
    3. # selects the word under the cursor
    4. my_cursor.select(QtGui.QTextCursor.WordUnderCursor)
    5.  
    6. # where the selction starts
    7. newpos = my_cursor.selectionStart()
    8.  
    9. # select from new_pos to old_pos (order is important)
    10. my_cursor.setPosition(newpos, QtGui.QTextCursor.MoveAnchor)
    11. my_cursor.setPosition(oldpos, QtGui.QTextCursor.KeepAnchor)
    12. self.editor.setTextCursor(my_cursor)
    13. SearchedText = my_cursor.selectedText()
    To copy to clipboard, switch view to plain text mode 

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.