Solved Exact Text fitting (Bug?)
-
Hi,
I am trying to tightly fit a rectangle around a Text element. There is some weird extra padding on top and bottom which I want to remove. This padding becomes bigger with increasing font size.
Rectangle { color: "red" width: text.contentWidth height: text.contentHeight } Text { id: text text: "ABC" font.pixelSize: 100 }
I tried an ugly workarund using TextMetrics as follows:
Rectangle { color: "red" width:metrics.tightBoundingRect.width height:metrics.tightBoundingRect.height } TextMetrics { id: metrics text: "ABC" font.pixelSize: 100 } Text { id: text text: "ABC" font.pixelSize: 100 topPadding: -(contentHeight - metrics.tightBoundingRect.height) / 2 }
but there is still some extra space at the bottom:
It seems to be a QT bug (https://bugreports.qt.io/browse/QTBUG-56052) known for about 1.5 years.
Is there a workaround (maybe with the help of C++), allowing for exact text metrics on all platforms, including high-DPI support?Best,
Alexej
-
You're setting
pixelSize
to 100 pixels, so rendered text will be that high. Just set your rectangle to 100px height, too. -
Thanks, but I found out that this behavior is normal. The space comes from the font itself and not from QML.