Results 1 to 3 of 3

Thread: Word at mouse click position in QGraphicsTextItem

  1. #1
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Word at mouse click position in QGraphicsTextItem

    I'm adding spell checking to my program, where text is edited inside QGraphicsTextItems. I got the underline working on mispelled words... Now I want the user to be able to right click on a word to get suggestions.. The problem is that I can't find any way to actually get what word is under the mouse cursor on a context event.

    QTextEdit has a function for getting the word at a position, but there doesn't seem to be any equivalent for QGraphicsTextItem... Obviously this must be a common task, since a left click will position the text cursor at the mouse click position, but I can't see anything in the API.. Am I missing something or is there no way to do this?

    Thanks,
    Leo

  2. #2
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Word at mouse click position in QGraphicsTextItem

    I've tracked down QTextControl::cursorForPosition(const QPointF &pos) in the private QTextControl class... Going to have a go at that one, but a public api would sure be nice..

  3. #3
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Word at mouse click position in QGraphicsTextItem

    Solved!

    Qt Code:
    1. QTextCursor c = textCursor();
    2. c.setPosition(document()->documentLayout()->hitTest(event->pos(), Qt::FuzzyHit));
    3. c.select(QTextCursor::WordUnderCursor);
    4. qDebug() << "selected text:" << c.selectedText();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Mouse position on screen
    By Nippler in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2008, 14:22
  2. QGraphicsTextItem: handling cursor position change
    By Angelo Moriconi in forum Qt Programming
    Replies: 2
    Last Post: 30th January 2007, 10:42

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.