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. How to make the width & height of a rectangle change dynamically with text.
Forum Updated to NodeBB v4.3 + New Features

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

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

    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?

    KroMignonK 1 Reply Last reply
    0
    • C Circuits

      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?

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #2

      @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
          }
      }
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      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