Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-43226

Text baseline does not follow selected vertical alignment although text is rendered correctly according to selected vertical alignment

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P2: Important
    • 5.6.0 Beta
    • 5.3.1, 5.4.0, 5.5.0
    • e255fdf207891765aa550709e084614242aebeec

    Description

      Tried to align two sibling texts of different font sizes on same baseline.
      The attached example only works ( i.e. both texts using the same baseline ) if vertical alignment is top - it results in strange baseline when alignment is bottom or vcenter ??
      The bigger text is aligned properly in any of the vertical alignments - it is just that the resulting baseline for the small text seems to be wrong for bottom/vcenter alignment.

      import QtQuick 2.2
      import QtQuick.Window 2.1
      
      Window {
          visible: true
          width:   220
          height:  220
      
          Item {
              id: theText
              anchors.fill:    parent
              anchors.margins: 10
      
              property int horizontalAlignment: Text.AlignLeft
              
              // result is ok for Top, but fails for VCenter and Bottom !?
              property int verticalAlignment:   Text.AlignVCenter
      
              Text {
                  id: major
                  text: "xx"
                  font.pixelSize:      40
                  height:              parent.height
                  horizontalAlignment: theText.horizontalAlignment
                  verticalAlignment:   theText.verticalAlignment
                  x: getX(theText.horizontalAlignment,theText.width,major.width,minor.width)
      
                  function getX() {
                      if( theText.horizontalAlignment == Text.AlignLeft )
                          return 0
                      if( theText.horizontalAlignment === Text.AlignHCenter )
                          return (theText.width - major.width - minor.width) / 2
                      if( theText.horizontalAlignment === Text.AlignRight )
                          return theText.width - major.width - minor.width
                  }
              }
              Text {
                  id: minor
                  text: ".yy"
                  anchors.left:     major.right
                  anchors.baseline: major.baseline
                  font.pixelSize:   20
              }
              Binding {
                  target:   major
                  property: "width"
                  value:    theText.width - minor.width
                  when:     theText.horizontalAlignment == Text.AlignRight
              }
              Binding {
                  target:   minor
                  property: "width"
                  value:    theText.width - major.width
                  when:     theText.horizontalAlignment == Text.AlignLeft
              }
      
      
              // Dummy border around texts to show the bounding boxes and baseline
              Rectangle {
                  anchors.fill: theText
                  color: Qt.rgba(0,0,0,0)
                  border { width: 1; color: "magenta" }
              }
              Rectangle {
                  anchors.fill: major
                  color: Qt.rgba(0,0,0,0)
                  border { width: 1; color: "blue" }
              }
              Rectangle {
                  anchors.fill: minor
                  color: Qt.rgba(0,0,0,0)
                  border { width: 1; color: "orange" }
              }
              Rectangle {
                  anchors.top:          major.baseline
                  anchors.bottom:       major.baseline
                  anchors.bottomMargin: -1
                  anchors.left:         theText.left
                  anchors.right:        theText.right
                  color: Qt.rgba(0,0,0,0)
                  border { width: 1; color: "pink" }
              }
              // end-of dummy borders
          }
      }
      

      Attachments

        1. qml2textalignment.tar.gz
          3 kB
        2. vcenter_stripped.png
          vcenter_stripped.png
          14 kB
        3. bottom.png
          bottom.png
          16 kB
        4. top.png
          top.png
          16 kB
        5. vcenter.png
          vcenter.png
          12 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            esabraha Eskil Abrahamsen Blomfeldt
            manuelkoch Manuel Koch
            Votes:
            4 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes