1

In order to extend /implement a custom widget with multiple lines (like a QTextEdit), I would need to convert widget coordinates to text position and text position to coordinates.

E.g. Mouse click on the text set the cursor position there. E.g.2 Using arrow keys will change the cursor position, which need to be drawn.

The drawing of the text is made with some line like:

painter.drawText(
    textRect, 
    Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap |Qt::TextJustificationForced, 
    text());

enter image description here

So if the text is about 130 characters, and the cursor position is at the position (e.g.) 60.

How to get the QPoint/QPointF at which this character 60 is drawn? If the user click at the position 110x16, which caracter is at this point?

I looked the function for QFontMetrics and QPainter, also the QTextCursor class. But I did not found any solution.

m.s.
  • 16,063
  • 7
  • 53
  • 88
Adrian Maire
  • 14,354
  • 9
  • 45
  • 85
  • http://stackoverflow.com/questions/13364231/qpainterdrawtext-get-bounding-boxes-for-each-character – SingerOfTheFall Oct 20 '15 at 07:55
  • Qt::TextJustificationForced and TextWordWrap could have quite complex behaviour. I cannot relay on per-character sizes. Or I would have to re-implement the drawText to ensure a coherent behaviour. – Adrian Maire Oct 20 '15 at 07:58

0 Answers0