Finding a piece of text under cursor
Solved
General and Desktop
-
So I need to find a piece of text under cursor. Now here's the problem, I already solved it when the user clicks on a word but now I need to grab the word when the user
hovers
over the word. I'm using PyQt5 version 5.12.1 and python 3.7.The method I used to get the word under cursor then user clicked is this:
Note:self
is a class that inherits from QPlainTextEdittextCursor = self.textCursor() textCursor.select(QTextCursor. print(textCursor.selectedText())
But now I wanna know how to get the same result without clicking
def mouseMoveEvent(self, QMouseEvent): print(QMouseEvent.pos())
My QPlainTextEdit class has mouse tracking turned on but I don't know how to proceed from here.
-
@Shoto Don't know for what you need that. But if this is for displaying a tooltip information about text under cursor, there is also another way to do it (sorry, C++ code again):
bool MyTextEdit::event(QEvent *e) { if (e->type() == QEvent::ToolTip) { QHelpEvent* helpEvent = static_cast<QHelpEvent*>(e); QString txt = cursorForPosition(helpEvent.pos()).select(QTextCursor::WordUnderCursor); if(txt.isEmpty()) return QPlainTextEdit::event(e); //Display tooltip at cursor position showToolTipForText(helpEvent->globalPos(), txt); return true; } return QPlainTextEdit::event(e); }