Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

text positioning/wrap conflict



  • Hi all -

    I've run into something that appears to be a Catch-22 with text formatting.

    Rectangle {
      height: root.highlightedBarHeight_
      width: root.highlightedBarWidth_ * 3
      radius: root.highlightedBarWidth_ / 10
      Text {
        anchors {
          horizontalCenter: parent.horizontalCenter
          verticalCenter: parent.verticalCenter
        }
        text: "remove cap"
        width: parent.width // need this to make wrapping work.
        wrapMode: Text.WordWrap
      }
    }
    

    The above code will not center the text in the rectangle, despite the anchors. I traced the problem to the presence of the width: line. BUT: if I remove the width line, wrapMode won't work, so the text can run off the side of the rectangle.

    I'm sure I'm not the first person to run across this, but...how does one go about getting centering and wrapping in the same element?

    Thanks...


  • Moderators

    @mzimmers

    Text {
        anchors {
          horizontalCenter: parent.horizontalCenter
          verticalCenter: parent.verticalCenter
        }
        text: "remove cap"
        horizontalAlignment: Text.AlignHCenter
        verticalAlignment: Text.AlignVCenter
        width: parent.width // need this to make wrapping work.
        wrapMode: Text.WordWrap
      }
    

Log in to reply