PDA

View Full Version : How to get current word in Qtextedit



iamjayanth
6th April 2009, 05:53
Hi masters

Can anyone help me to find the on which word the cursor is now on QTextEdit. I couldnt even find the current character. I tried to use charAt() function but but I dont know how to get current cursor position(QPoint variable.).Hoping for any help.

Thanking you
Jayanth.S

spirit
6th April 2009, 06:04
try this


...
QTextCursor tc = m_textEdit->textCursor();
tc.select(QTextCursor::WordUnderCursor);
QString word = tc.selectedText();
...

iamjayanth
6th April 2009, 06:25
Thanks for the quick reply...But When I use that code I am getting errors


error: variable ‘QTextCursor tc’ has initializer but incomplete type
error: ‘QTextCursor* QTextEdit::textCursor() const’ is protected
error: incomplete type ‘QTextCursor’ used in nested name specifier

I tried to find an header file for QTextCursor but failed ... What am I missing...

spirit
6th April 2009, 06:32
what version of Qt do you use?
looks like Qt 3, but in you info you use Qt 4 :)
so, my code for Qt 4.

iamjayanth
6th April 2009, 06:43
I am using QT 3.3... Is there any other methods , which work on QT 3.3 also...Atleast if I can get the current character then I can parse out the word...Help me...

Jayanth.S

wysota
6th April 2009, 08:57
Use QTextEdit::getCursorPosition() to get the index of the paragraph where the cursor is positioned and then use QTextEdit::text() to get the text of that paragraph. Then you can find the word the cursor is at using the position in the paragraph retrieved with the first call.

BTW. You might want to update your forum profile if you use Qt3. Currently it says you are using Qt4 only.