QML自动换行以及TextArea和页边距

时间:2019-05-15 12:42:32

标签: qt qml

如何使我的TextArea文字自动换行并自动调整其高度以在TextArea中显示可用的文本?其次,如何去除附件中看到的奇数间距。

这就是我现在拥有的:

enter image description here

这就是我想要的:

enter image description here

QML代码:

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3

Frame {
    anchors.centerIn: parent
    anchors.fill: parent

    ListView {
        implicitWidth: parent.width
        implicitHeight: parent.height
        clip: true

        model: ListModel {
            ListElement {
                description: "Wash the car this could be a really long message with some multiline support we will see how it works."
            }
            ListElement {
                description: "Fix the car"
            }
            ListElement {
                description: "Sell the car"
            }
            ListElement {
                description: "Wash the car this could be a really long message with some multiline support we will see how it works. This should word wrap quite a lot of times."
            }
            ListElement {
                description: "Fix the car"
            }
            ListElement {
                description: "Sell the car"
            }
            ListElement {
                description: "Wash the car"
            }
            ListElement {
                description: "Fix the car"
            }
            ListElement {
                description: "Sell the car"
            }
        }

        delegate: RowLayout {
            Layout.fillWidth: true

            Rectangle {
                id: newsicon
                width: 16
                height: 16
                color: "steelblue"
                Layout.alignment: Qt.AlignTop
            }

            ColumnLayout {
                Layout.fillWidth: true
                spacing: 0

                TextArea {
                    Layout.fillWidth: true
                    Layout.alignment: Qt.AlignTop
                    id: messageText
                    text: model.description
                    wrapMode: TextEdit.WordWrap
                    readOnly: true
                    textMargin: 0.0
                    background: null
                }
                Label {
                    id: dateText
                    text: "Dec 20, 2019"
                    font.italic: true
                    font.pointSize: 8
                    color: "grey"
                }
            }
        }

        ScrollBar.vertical: ScrollBar {
            active: true
        }
    }
}

0 个答案:

没有答案