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. Rectangle with dynamic text size doesn't center

Rectangle with dynamic text size doesn't center

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 439 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.
  • D Offline
    D Offline
    Djoe
    wrote on last edited by
    #1

    Hello,

    I am trying to center a rectangle with text. Once the text has changed the rectangle is not centered anymore. I suppose I have a binding issue. The first iteration works correctly but not the second one.
    Bellow a simple example of my problem :

    Page
    {
        property bool temp : false
        RowLayout
        {
            id: mainLayout
    
            anchors.centerIn: parent
    
            Rectangle
            {
                id: rectangle
    
    
                width: labelText.paintedWidth + 16
                height: labelText.paintedHeight + 6
    
                radius: 8
                color: "red"
    
                Label
                {
                    Timer {
                            interval: 500; running: true; repeat: true
                            onTriggered:
                            {
                                if (temp == false)
                                    labelText.text = "test"
    
                                if (temp === true)
                                    labelText.text = "Why doesn't it center ?"
    
                                temp = !temp
                            }
                        }
    
                    id: labelText
                    anchors.centerIn: parent
                    color: "black"
                    text: "test"
                    font.pixelSize: 70
    
                    font.bold: true
                }
            }
        }
    }
    

    Result 1 iteration:
    1iteration.png

    Result 2 iteration:
    2iteration.png

    For information, on my current software I use Q_PROPERTY to update the text in the rectangle. But I think the problem stays the same.

    CP71C 1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      Don't specify width and height in a Layout. Use Layout.preferredWidth (or implicitWidth) and Layout.preferredHeight (or implicitHeight) instead.

      D 1 Reply Last reply
      1
      • D Djoe

        Hello,

        I am trying to center a rectangle with text. Once the text has changed the rectangle is not centered anymore. I suppose I have a binding issue. The first iteration works correctly but not the second one.
        Bellow a simple example of my problem :

        Page
        {
            property bool temp : false
            RowLayout
            {
                id: mainLayout
        
                anchors.centerIn: parent
        
                Rectangle
                {
                    id: rectangle
        
        
                    width: labelText.paintedWidth + 16
                    height: labelText.paintedHeight + 6
        
                    radius: 8
                    color: "red"
        
                    Label
                    {
                        Timer {
                                interval: 500; running: true; repeat: true
                                onTriggered:
                                {
                                    if (temp == false)
                                        labelText.text = "test"
        
                                    if (temp === true)
                                        labelText.text = "Why doesn't it center ?"
        
                                    temp = !temp
                                }
                            }
        
                        id: labelText
                        anchors.centerIn: parent
                        color: "black"
                        text: "test"
                        font.pixelSize: 70
        
                        font.bold: true
                    }
                }
            }
        }
        

        Result 1 iteration:
        1iteration.png

        Result 2 iteration:
        2iteration.png

        For information, on my current software I use Q_PROPERTY to update the text in the rectangle. But I think the problem stays the same.

        CP71C Offline
        CP71C Offline
        CP71
        wrote on last edited by CP71
        #3

        @Djoe
        try add in rectangle obj;

        anchors.centerIn: parent

        fb954464-1eda-4dcc-86d5-32339b82ede7-image.png

        Code:

        Page
        {
        property bool temp : false
        RowLayout
        {
        id: mainLayout

            anchors.centerIn: parent
        
            Rectangle
            {
                id: rectangle
        
                anchors.centerIn: parent
        
                width: labelText.paintedWidth + 16
                height: labelText.paintedHeight + 6
        
                radius: 8
                color: "red"
        
                Label
                {
                    Timer {
                            interval: 1000; running: true; repeat: true
                            onTriggered:
                            {
                                if (temp == false)
                                    labelText.text = "test"
        
                                if (temp === true)
                                    labelText.text = "Why doesn't it center ?"
        
                                temp = !temp
                            }
                        }
        
                    id: labelText
                    anchors.centerIn: parent
                    color: "black"
                    text: "test"
                    font.pixelSize: 70
        
                    font.bold: true
                }
            }
        }
        

        }

        1 Reply Last reply
        0
        • GrecKoG GrecKo

          Don't specify width and height in a Layout. Use Layout.preferredWidth (or implicitWidth) and Layout.preferredHeight (or implicitHeight) instead.

          D Offline
          D Offline
          Djoe
          wrote on last edited by Djoe
          #4

          @GrecKo Thank you for your answer. It works perfectly !

          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