Hello,
In a sub-classed QTextEdit widget, I need to locate the character underneath the mouse pointer both for showing a tool tip and to construct the contextual menu.
Following the Qt documentation (and as suggested in all the posts of these forums I could locate on the topic), I re-implemented event() (for tool tips) and contextMenuEvent() (for the menu) and used cursorForPosition( event->pos() ).position() to get the character at the mouse pointer (actually the index of character within the widget text string).
The code works correctly for all details, except one:
The range of mouse pointer positions returning a given character does not correspond to the bounding box of that character but it is shifted left by roughly half of the character width.
A practical example:
Assuming the widget contains the text "ABCD", cursorForPosition() returns:
* 'A' position when the mouse pointer is over the left half of the 'A';
* 'B' position when the mouse pointer is over the right half of the 'A' or the left half of the 'B';
* 'C' position when the mouse pointer is over the right half of the 'B' or the left half of the 'C';
* 'D' position when the mouse pointer is over the right half of the 'C' or the left half of the 'D';
* <nothing> (an invalid index) when the cursor is over the right half of the 'D'.
(I may assume these be the positions where a textual cursor would be placed by clicking 'there' with mouse). The UI result is puzzling!
The questions are:
1) Are there method(s) actually dealing with position(s) of textual units (characters) and not of cursors? (the two being different as shown above)
2) Can I translate cursorForPosition() returned value to a character without reconstructing character width(s) from the textual format and/or the font metrics? (cursorForPosition() is already doing this for me! Why do it again?)
Thanks,
M.
Bookmarks