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. Measuring width of rich text with TextMetrics
Qt 6.11 is out! See what's new in the release blog

Measuring width of rich text with TextMetrics

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 1.2k 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.
  • T Offline
    T Offline
    Trian
    wrote on last edited by
    #1

    When text contains line break, TextMetrics width returns width as if there is no line break in text. FontMetrics behaves the same.

    import QtQuick 2.11
    import QtQuick.Window 2.11
    
    Window {
        id: mainWindow
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        TextMetrics {
            id: textMetrics
            font.family: "Arial"
            elide: Text.ElideMiddle
            elideWidth: 100
            font.pointSize: 14
            text: "Hello<br>World!!!"
        }
    
        FontMetrics {
            id: fontMetrics
            font.family: "Arial"
            font.pointSize: 14
        }
    
        Rectangle {
            anchors.centerIn: parent
            border.width: 1
            width: 300
            height: 200
            Text {
                anchors.centerIn: parent
                font : textMetrics.font
                text: textMetrics.text
            }
        }
    
        Rectangle {
            color: "yellow"
            opacity: 0.5
            anchors.centerIn: parent
            width: textMetrics.width
            height: textMetrics.height
        }
    }
    
    
    1 Reply Last reply
    1
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      What's your goal ?

      You can get the actual text dimensions of an existing Text item with contentWidth and contentHeight.

      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