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. Exact Text fitting (Bug?)
Forum Update on Monday, May 27th 2025

Exact Text fitting (Bug?)

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 755 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.
  • A Offline
    A Offline
    alexej.k
    wrote on last edited by
    #1

    Hi,

    I am trying to tightly fit a rectangle around a Text element. There is some weird extra padding on top and bottom which I want to remove. This padding becomes bigger with increasing font size.

    alt text

    Rectangle {
            color: "red"
            width: text.contentWidth
            height: text.contentHeight
    
        }
    
        Text {
            id: text
            text: "ABC"
            font.pixelSize: 100
    
        }
    

    I tried an ugly workarund using TextMetrics as follows:

     Rectangle {
            color: "red"
            width:metrics.tightBoundingRect.width
            height:metrics.tightBoundingRect.height
    
        }
        TextMetrics {
            id: metrics
            text: "ABC"
            font.pixelSize: 100
        }
    
        Text {
            id: text
            text: "ABC"
            font.pixelSize: 100
            topPadding: -(contentHeight - metrics.tightBoundingRect.height) / 2
    
        }
    

    but there is still some extra space at the bottom:

    alt text

    It seems to be a QT bug (https://bugreports.qt.io/browse/QTBUG-56052) known for about 1.5 years.
    Is there a workaround (maybe with the help of C++), allowing for exact text metrics on all platforms, including high-DPI support?

    Best,

    Alexej

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

      You're setting pixelSize to 100 pixels, so rendered text will be that high. Just set your rectangle to 100px height, too.

      (Z(:^

      1 Reply Last reply
      0
      • A Offline
        A Offline
        alexej.k
        wrote on last edited by
        #3

        Thanks, but I found out that this behavior is normal. The space comes from the font itself and not from QML.

        1 Reply Last reply
        1

        • Login

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