简体   繁体   中英

How to make qml TableView row height dynamically adapt to content

The problem is the following:

If the length of the text is longer than the width of the cell, the text is wrapped, but the height of the row is not increased. which displays the rest of the text chopped. My other question is, how to adapt the height of each cell to the text contained in it?

屏幕截图

Here's the QML part:

Window {
id: window
visible: true
width: 440
height: 400
title: qsTr("Table test")

ListModel {
    id: stringsModel

    ListElement {
        ID: 0
        String: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam'
    }
    ListElement {
        ID: 1
        String: 'This is a test string'
    }ListElement {
        ID: 1
        String: 'This is another test string'
    }
}

TableView {
    anchors.fill: parent
    frameVisible: false
    model: stringsModel

    TableViewColumn { role: "ID"; title: "ID"; width: window.width / 2 }
    TableViewColumn { role: "String"; title: "String"; width: window.width / 2; delegate: stringDelegate;}

    Component {
        id: stringDelegate
        Item {
            id: stringItem
            Text {
                id: stringTxt
                width: parent.width
                text: styleData.value
                wrapMode: TextEdit.WordWrap
            }
        }
    }
}

}

Your delegate's root item should define its implicitHeight property. Something like this:

Component {
    id: stringDelegate
    Item {
        id: stringItem
        implicitHeight: stringTxt.paintedHeight
        Text {
            id: stringTxt
            width: parent.width
            text: styleData.value
            wrapMode: TextEdit.WordWrap
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM