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. dynamic margin according to wrapped text
Qt 6.11 is out! See what's new in the release blog

dynamic margin according to wrapped text

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 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.
  • O Offline
    O Offline
    OPit44
    wrote on last edited by
    #1

    Hi ,
    is there a way to dynamically change margins of a Label when the text is wrapped or not ?
    I try this one but it doesn't work :

    Button {
        id: root
        width: 146
        height: 60
       
      background: Rectangle {
            id: buttonBackground
            anchors.fill: parent
            color: "#333"
            border.width: 0
            radius: height / 2
        }
    
       contentItem: Label {
            text: root.text
            font: root.font
            anchors.fill: parent
            anchors.leftMargin: contentWidth > width ? 12 : 4
            anchors.rightMargin:  contentWidth > width ? 12 : 4
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            wrapMode: Label.Wrap
            elide: Text.ElideRight
            color: CustomColor.hightlightedText
        }
    

    Any idea ?
    Thanks

    1 Reply Last reply
    0
    • crimson1023C Offline
      crimson1023C Offline
      crimson1023
      wrote on last edited by
      #2

      How does that code work on your end?
      It seems to work fine...
      What do you want actually?

      1 Reply Last reply
      0
      • O Offline
        O Offline
        OPit44
        wrote on last edited by
        #3

        contentWidth > width not working, it's always 4

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

          Try using lineCount > 1 if you want to check whether the text is wrapped.

          1 Reply Last reply
          0
          • O Offline
            O Offline
            OPit44
            wrote on last edited by OPit44
            #5

            Yes great, thank you.
            I add another thing which is to take into consideration the number of lines once the component is completed, because if I manage the margin directly during initialization, it does not work:

            anchors.leftMargin: contentWidth > width ? 12 : 4
            anchors.rightMargin: contentWidth > width ? 12 : 4

            ...
            Component.onCompleted: {
                        anchors.leftMargin = (lineCount > 1 ? 12 : 4)
                        anchors.rightMargin = (lineCount > 1 ? 12 : 4)
                    }
            
            1 Reply Last reply
            0
            • O OPit44 has marked this topic as solved on

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved