PDA

View Full Version : coordinates of selected text in QTextEdit



sreedhar
15th May 2006, 12:09
hi,
I want to get the cooordinates of the selected text in QTextEdit, any one corner of the of the selected text.

Please post code snippet if possible

Thank you,

regards,
sreedhar

wysota
15th May 2006, 17:22
This might be a little complicated (and it hasn't been tested)...

Try using QTextEdit::cursorRect(). If we assume that the cursor always touches one of the ends of the selection, you'll get your coordinates correctly.

If it doesn't, you can retrieve the position of the selection using QTextCursor::selectionStart() (or selectionEnd), then get a QTextBlock object using QTextDocument::findBlock(). Then get the layout using QTextBlock::layout() which has a QTextLayout::boundingRect() method, which will give you the coordinates of the block or QTextLayout::position() which will return the exact point.

These coordinates should be correct when taken as the viewport coordinates of the text edit widget.

But as I said -- no warranty this will work. It's just a logical chain of calls. It might result in something completely different.