-1

I have a problem with a Rectangle that should contain Text. Text should be in the Rectangle defined but it goes beyond it.

import QtQuick 2.12
import QtQuick.Window 2.12

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Rectangle
    {
        id:listRest
        width: parent.width/2.7
        height: parent.height/2.5
        color: "dimgray"
        anchors.left: parent.left
        anchors.topMargin: Math.round(parent.height)/12
        anchors.margins: Math.round(parent.width)/50

        Text {
            anchors.fill: parent
            anchors.centerIn: parent
            text: qsTr("QML (Qt Modeling Language) is a user interface markup language. It is a declarative language for designing user interface–centric applications.")
        }
    } 
}

How can I get Text to be only in Rectangle?

ImNew
  • 97
  • 9

2 Answers2

1

By default, the Text will display its content as it is. So, if the text doesn't contain enough new line and it's too long, the text will be display outside the rectangle.

Use wrapMode: Text.WordWrapto wrap the text in the bounds of the rectangle and elide: Text.ElideRight to elide the text if it's too long to fit in the rectangle.

Dimitry Ernot
  • 6,256
  • 2
  • 25
  • 37
0

Using a wrapMode (a max width)

Or maybe a simple solution that is not elegant, but work. Use "\n" for a new line when is it possible without other over-complicated methods if is just only for a simple small text line.