PDA

View Full Version : Word at mouse click position in QGraphicsTextItem



pherthyl
3rd November 2008, 00:30
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

pherthyl
3rd November 2008, 03:53
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..

pherthyl
3rd November 2008, 05:56
Solved!



QTextCursor c = textCursor();
c.setPosition(document()->documentLayout()->hitTest(event->pos(), Qt::FuzzyHit));
c.select(QTextCursor::WordUnderCursor);
qDebug() << "selected text:" << c.selectedText();