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. [Partially solved] QML Text seems to be horizontally aligned wrong
Forum Updated to NodeBB v4.3 + New Features

[Partially solved] QML Text seems to be horizontally aligned wrong

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 9.6k 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.
  • I Offline
    I Offline
    imihajlov
    wrote on last edited by
    #1

    I'm trying to make a button with centered text inside, but I have a following problem:
    !http://s15.postimage.org/pnmyox5yv/Screenshot_at_2012_08_17_12_20_56.png(misaligned text)!
    As you can see, the Text element is centered correctly (its dimensions are shown by the black frame), but the real text inside it is painted a bit left. It doesn't depend on font. What should I do to make it centered correctly?
    Code:
    @ Text {
    id: label
    text: toplevel.text
    font.pixelSize: toplevel.height * 0.4
    anchors.horizontalCenter: parent.horizontalCenter
    anchors.baseline: alignRect.bottom
    }
    Item {
    id: alignRect
    visible: false
    anchors.centerIn: parent
    width: 10
    height: label.font.pixelSize
    }
    Rectangle {
    color: "transparent"
    border.width: 1
    anchors.fill: label
    }@

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Try this:
      @
      Text {
      id: label
      text: toplevel.text
      font.pixelSize: toplevel.height * 0.4
      anchors.horizontalCenter: parent.horizontalCenter
      anchors.baseline: alignRect.bottom

          // this:
          width: paintedWidth
          height: paintedHeight
          // and this:
          verticalAlignment: Text.AlignVCenter
          horizontalAlignment: Text.AlignHCenter
      }
      

      @

      (Z(:^

      1 Reply Last reply
      0
      • I Offline
        I Offline
        imihajlov
        wrote on last edited by
        #3

        Thank you, it helped a bit - now the text is less misaligned!
        !http://s14.postimage.org/dfyjzpwfx/Screenshot_at_2012_08_17_13_47_35.png(new result)!
        But it isn't the ideal result yet :(

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Add anchor margins :)

          (Z(:^

          1 Reply Last reply
          0
          • I Offline
            I Offline
            imihajlov
            wrote on last edited by
            #5

            Yeah, that dirty way should work, but isn't there a clean way to do this and why does this stuff happen?

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              Glyph painting is a very hard job in general. Ultimately, that is probably a Qt bug, or type bug (glyph definition). You have to remember, that some letters are allowed to go beyond their registered space (take a 'y' for example, in some types, and especially in handwriting, it often even obscures other letters), others are allowed to join, etc.

              I had that problem in my code, too, I've added the aligning as above + adjusted the margins by hand. It seems to be working well, even during scaling, and on different operating systems.

              (Z(:^

              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