Text Alignment



  • Hi All,

    I want to align text to the center of rectangle if the length of the string is less.
    Or else i need to make the text to fit into the parent.

    How do I achieve it.
    Below is the code that tried, But unable to align it to center when the text is less in length

    Rectangle{
         width:200
         height:60
         color: "lightgray"
         Text{
             anchors.fill: parent
             font.pixelSize: 24
             text: "Some Some" /*"SOme Text SOme Text SOme Text SOme text sdjhsakcjcsdkvkjds"*/
             fontSizeMode: Text.Fit
             minimumPixelSize: 10
             wrapMode: Text.WordWrap
    
            horizontalAlignment:  parent.horizontalCenter
            verticalAlignment: parent.verticalCenter
         }
     }
    

    Edit[j.hilk]:added missing code tags for better reading


  • Moderators

    hi @Madesh-R

    that are not the correct binding for text alignment

    Rectangle{
       width:200
       height:60
       color: "lightgray"
       Text{
           anchors.fill: parent
           font.pixelSize: 24
           text: "Some Some" /*"SOme Text SOme Text SOme Text SOme text sdjhsakcjcsdkvkjds"*/
           fontSizeMode: Text.Fit 
           minimumPixelSize: 10
           wrapMode: Text.WordWrap
    
           horizontalAlignment: Text.AlignHCenter
           verticalAlignment: Text.AlignVCenter
      }
    }
    


  • @J.Hilk

    Thank you Hilk. It's working... :)


  • Moderators

    @Madesh-R
    great, and you're welcome ;-)

    Don't forget to set the top to solved, is your issue has indeed been solved ;-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.