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. Proper use of anchors.baseline
Qt 6.11 is out! See what's new in the release blog

Proper use of anchors.baseline

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 934 Views
  • 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.
  • B Offline
    B Offline
    bitbang
    wrote on last edited by bitbang
    #1

    I need to display lots of meter readings, e.g. "440 hertz" and sometimes to be a more fancy or descriptive, like "frequency: 440 hertz" possibly on a flashing background. Starting is easy enough:

    Item {
        Text {
            id: description
            text: "frequency:"
            verticalAlignment: Qt.AlignCenter
        }
    
        Row {
            anchors.baseline: description.baseline
            anchors.left: description.right
            width: implicitWidth
            id: reading
            Text {
                id: value
                text: "440"
                anchors.baseline: reading.baseline
            }
            Text {
                id: units
                text: "hertz"
                anchors.baseline: value.baseline
            }
        }
    

    (to keep this example simple I kept Row inline instead of moving it to a separate Component).
    And it works! But let's add some color:

    Item {
        Rectangle {
            anchors.fill: reading
            color: "red"
        }
        Rectangle {
            anchors.fill: description
            color: "green"
        }
        //code from here is the same as in previous example
        Text {
            id: description
            text: "frequency:"
            verticalAlignment: Qt.AlignCenter
        }
        Row {
            anchors.baseline: description.baseline
            anchors.left: description.right
            width: implicitWidth
            id: reading
            Text {
                id: value
                text: "440"
                anchors.baseline: reading.baseline
            }
            Text {
                id: units
                text: "hertz"
                anchors.baseline: value.baseline
            }
        }
    

    Now it's looking weird...
    baseline.png
    The documentation clearly states that baselines for non-text objects (like Row) equals top, so this behavior is by design and not a QML bug.
    Therefore I'm doing it wrong: so my question is what's the proper way to extend baseline alignment while traversing non-text containers? My goal is to write a 'Reading' component to be used alone, or as a bulding block of a more complete object (adding description, flashing background to name a few).

    1 Reply Last reply
    0
    • GrecKoG Online
      GrecKoG Online
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      The solution is to set the baselineOffset property of your Row to one of your nested Text object:

      baselineOffset: value.baselineOffset

      You can then use anchors.baseline with it.

      B 1 Reply Last reply
      4
      • GrecKoG GrecKo

        The solution is to set the baselineOffset property of your Row to one of your nested Text object:

        baselineOffset: value.baselineOffset

        You can then use anchors.baseline with it.

        B Offline
        B Offline
        bitbang
        wrote on last edited by
        #3

        @GrecKo Beautiful solution, for some unknown reason I was erroneously thinking of the baseline like a sort of read-only property dictated by font geometries, instead of something that can be moved. Cleaner and simpler, thank you!

        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