Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML 5.15 Align Text to top in parent not working
Forum Updated to NodeBB v4.3 + New Features

QML 5.15 Align Text to top in parent not working

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 511 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VFCraig
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • MarkkyboyM Offline
      MarkkyboyM Offline
      Markkyboy
      wrote on last edited by
      #2

      You are more likely to get help/answers if you post the code you have, this way we can likely see where perhaps you're going wrong, otherwise, it is guess work :/

      Don't just sit there standing around, pick up a shovel and sweep up!

      I live by the sea, not in it.

      1 Reply Last reply
      0
      • V Offline
        V Offline
        VFCraig
        wrote on last edited by
        #3

        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: 0

          text: "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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved