+ Reply to Thread
Results 1 to 5 of 5

Thread: QTextEdit: get word under the mouse pointer?

  1. #1
    Join Date
    Jan 2007
    Posts
    95
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default QTextEdit: get word under the mouse pointer?

    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

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,507 Times in 1,384 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTextEdit: get word under the mouse pointer?

    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.
    J-P Nurmi

  3. #3
    Join Date
    Jan 2007
    Posts
    95
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextEdit: get word under the mouse pointer?

    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:

    Qt Code:
    1. QTextCursor tc = textCursor();
    2. tc.select(QTextCursor::WordUnderCursor);
    3. this->setToolTip(getToolTip(tc.selectedText()));
    To copy to clipboard, switch view to plain text mode 

    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.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,507 Times in 1,384 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTextEdit: get word under the mouse pointer?

    Catch QEvent::ToolTip and use QToolTip::showText().

    Edit: try something like this
    Qt Code:
    1. bool MyTextEdit::event(QEvent* event)
    2. {
    3. if (event->type() == QEvent::ToolTip)
    4. {
    5. QHelpEvent* helpEvent = static_cast<QHelpEvent*>(event);
    6. QTextCursor cursor = cursorForPosition(helpEvent->pos());
    7. cursor.select(QTextCursor::WordUnderCursor);
    8. if (!cursor.selectedText().isEmpty())
    9. QToolTip::showText(helpEvent->globalPos(), cursor.selectedText());
    10. else
    11. QToolTip::hideText();
    12. return true;
    13. }
    14. return QTextEdit::event(event);
    15. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 11th February 2008 at 13:18. Reason: updated contents
    J-P Nurmi

  5. The following 2 users say thank you to jpn for this useful post:

    Ev_genus (20th February 2011), zorro68 (11th February 2008)

  6. #5
    Join Date
    Jul 2009
    Posts
    42
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: QTextEdit: get word under the mouse pointer?

    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

+ Reply to Thread

Similar Threads

  1. Re-implement mouse events of QTextEdit
    By Ankitha Varsha in forum Qt Programming
    Replies: 2
    Last Post: 14th October 2006, 16:55

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