QML 5.15 Align Text to top in parent not working
-
This should be very simple. And it is possible my interpretation of the problem is incorrect.
But I created a Text object and set the anchors for Left and Top to the parent's Left and Top. The result was left justified within the parent, but in the Y axis it sat well below the top.
Looking finally at the FontMetrics for the font being used, it very much appears that when anchoring to Top, a Text object incorporates the font "descent" value. The descent value being the distance from the baseline of the font to the lowest point of characters such as, "g", "j" or "y".
In other words, it has no use what so ever in calculating the top of a Text object.
Back in the Qt3 days, the documentation included many clear diagrams to present the visual results of different GUI actions. The font metrics included a clear diagram of the different lines of a font, showing clearly how descent, ascent, baseline, etc. related to a font and text using it.
The parent is an Item, with margins of 0. The Text object has padding set to 0. The verticalAlignment and horizontalAlignment are set to Text.AlignTop and Text.AlignLeft respectively.
To get the Text to align to the top I had to set the anchors.topMargin to (-fm.descent), where fm is the font metrics.
I know this is not a correct solution. It may just be coincidence that the descent value is the exact offset required to get the text to align to top. Every example I have found in the QML docs seems to focus on center alignment, which works fine.
Any assistance and/or clarification would be greatly appreciated. Thanks.
-
If it was something more than just a few simple lines I would. As this is the QML forum and those likely to have an answer could easily write the lines, I saw no reason.
Item
{
Text
{
anchors.left: parent.left
anchors.top: parent.top
verticalAlignment: Text.AlignTop
horizontalAlignment: Text.AlignRight
padding: 0text: "This text does not align to the top"
}
}Subtracting the descent value from the font metrics for the font used from the topMargin got the text to align to the top.