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. Trouble with text alignment

Trouble with text alignment

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 415 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 Circuits
    #1

    I am attempting to get the text to align in the center of this rectangle but despite setting anchors.horizontalCenter too parent.horizontalCenter it is still aligning to the left?

    0_1568746868690_theimage.png

                Rectangle {
                    id: display
                    color: "white"
                    height: componentHeight
                    width: componentWidth
                    radius: 5
    
                    TextStyled {
                        id: entryDisplay
                        elide: Text.ElideRight
                        width: parent.width
                        style: fontStyle
                        anchors.horizontalCenter: parent.horizontalCenter
                        anchors.bottom: display.bottom
                        color: modelData.settingInvalid ? "red" : "gray"
                        text: ""
                        Component.onCompleted: {
                            if(modelData.settingInvalid)
                            {
                                entryDisplay.text = modelData.value
                                value_container.allowNext = false
                            } else
                            {
                                entryDisplay.text = modelData.value
                            }
                        }
                    }
                }
    

    EDIT: Here is TextStyled:

    Text {
        id: control
    
        property BaseTextStyle style: Style1TextStyle {}
    
        font: control.style.font
        color: control.style.color
    }
    
    1 Reply Last reply
    0
    • IntruderExcluderI Offline
      IntruderExcluderI Offline
      IntruderExcluder
      wrote on last edited by
      #2

      anchors.horizontalCenter centers the whole item inside another, since your TextStyled have same width as rectangle everyting is working fine. You can remove width property or set horizontalAligment: Text.AlignHCenter. It is very useful to add background colored rectangles to an items to see how are they placed:

      Rectangle {
          z: -1
          anchors.fill: parent
          color: "teal"
      }
      

      Add this rectangle at the end of your text element to see how much space occupied by your text item.

      C 1 Reply Last reply
      3
      • IntruderExcluderI IntruderExcluder

        anchors.horizontalCenter centers the whole item inside another, since your TextStyled have same width as rectangle everyting is working fine. You can remove width property or set horizontalAligment: Text.AlignHCenter. It is very useful to add background colored rectangles to an items to see how are they placed:

        Rectangle {
            z: -1
            anchors.fill: parent
            color: "teal"
        }
        

        Add this rectangle at the end of your text element to see how much space occupied by your text item.

        C Offline
        C Offline
        Circuits
        wrote on last edited by
        #3

        @intruderexcluder Excellent thank you for that, very insightful!

        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