Important: Please read the Qt Code of Conduct -

How to make the width & height of a rectangle change dynamically with text.

  • I want to contain some text in a Rectangle but the text string may change and if it does I want the Rectangle to change it's size accordingly. Anyone have an idea about how I might do that? Perhaps someone could point me to a good example?

  • @Circuits To do this, the easiest way is to use TextMetrics.
    Something like this:

    Rectangle {
        property alias text: _label.text
        implicitWidth: tm.width
        implicitHeight: tm.height
        Label {
            id: _label
            anchors.centerIn: parent
        TextMetrics {
            id: tm
            font: _label.font
            text: _label.text

Log in to reply