PDA

View Full Version : QTextEdit: get word under the mouse pointer?



zorro68
11th February 2008, 01:06
how can I get the word under the mouse?

(this is to get the word and show in a tooltip the help of this command, p.e.)

Thanks

jpn
11th February 2008, 07:44
Start with looking at QTextEdit docs. According to your problem description, you need a methods which takes a QPoint. You'll find a method which gives you a QTextCursor. The next step is to dig into QTextCursor docs. Search for "word" and you'll find QTextCursor::WordUnderCursor. The rest is left as an exercise for the reader.

zorro68
11th February 2008, 12:29
Start with looking at QTextEdit docs. According to your problem description, you need a methods which takes a QPoint. You'll find a method which gives you a QTextCursor. The next step is to dig into QTextCursor docs. Search for "word" and you'll find QTextCursor::WordUnderCursor. The rest is left as an exercise for the reader.


I have read these helps, and I use this code in a mouseMoveEvent:



QTextCursor tc = textCursor();
tc.select(QTextCursor::WordUnderCursor);
this->setToolTip(getToolTip(tc.selectedText()));


but this work if the cursor is on/into the word. But I want that although the cursor is in other place, only moving the mouse pointer onto the word, gets the word below the mouse pointer.

jpn
11th February 2008, 13:02
Catch QEvent::ToolTip and use QToolTip::showText().

Edit: try something like this


bool MyTextEdit::event(QEvent* event)
{
if (event->type() == QEvent::ToolTip)
{
QHelpEvent* helpEvent = static_cast<QHelpEvent*>(event);
QTextCursor cursor = cursorForPosition(helpEvent->pos());
cursor.select(QTextCursor::WordUnderCursor);
if (!cursor.selectedText().isEmpty())
QToolTip::showText(helpEvent->globalPos(), cursor.selectedText());
else
QToolTip::hideText();
return true;
}
return QTextEdit::event(event);
}

ericV
13th November 2009, 15:42
Hi,

I know this is an older thread, but it fits. I am trying to do something similar in QT4.5.

I have a QTextEdit in which i want to show information whenever the user hovers on the text.
But i have the following problem:

- the tooltip apears almost one line below the cursor,
- when the mouse is on a blank area, the tooltip shows the last word of the line or text.

Is there any way to get the text cursor position in viewport coordinates, in order to make sure that the mouse cursor is "close" to it?

Thanks,

Eric