PDA

View Full Version : QTextCursor::WordUnderCursor



kib2
8th September 2007, 13:56
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.

wysota
8th September 2007, 14:17
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.

kib2
8th September 2007, 15:04
Thanks,

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


oldpos = my_cursor.position()

# selects the word under the cursor
my_cursor.select(QtGui.QTextCursor.WordUnderCursor )

# where the selction starts
newpos = my_cursor.selectionStart()

# select from new_pos to old_pos (order is important)
my_cursor.setPosition(newpos, QtGui.QTextCursor.MoveAnchor)
my_cursor.setPosition(oldpos, QtGui.QTextCursor.KeepAnchor)
self.editor.setTextCursor(my_cursor)
SearchedText = my_cursor.selectedText()