Search code examples
qtqtquickcontrols

How can I get the width of each line of text in a label (the last line in particular)?


I have a label in my app that displays multiline text by setting wrapMode: Text.WordWrap. Is there any way to calculate the location (x and y) of the end of the last line of text in the label given a constrained width? In other words, if my label is constrained to be 100 px width and the last line of text (out of three lines) ends two-thirds of the way over, I'd like to be able to know that the end of the last line is at 66 px and that the third line starts with a y-value of 40 (if, for example, each line is 20 px high).

My requirement is that I need to place a little icon/image exactly at the end of the last line of text.


Solution

  • Label doesn't appear to have any way to get this, but TextArea has the method positionToRectangle(position) which gives the exact location of any character in the text and is exactly what I needed.