动态分开更改QML TableView元素行高度

时间:2016-04-12 13:26:24

标签: qt qml tableview

在QML TableView中有一个简单的方法来修改行高吗?

我的问题是我动态加载数据是TableView的模型。我有一个"警告"我想在列中显示使用Qt RichText HTML标记(ul,li)的警告列表。但是,此列表高于包含它的单元格,并且存在y溢出。

QTableView类有很多方法可以解决这个问题,比如setRowHeight(int row,int height),resizeRowToContents(int row)或resizeRowsToContents()。

但似乎QML等价物没有这样的方法来轻松调整行的大小......

rowDelegate可能会解决我的问题,但我不知道如何使用它来修改行'单独的高度(我的意思是给它的索引)。

有没有人遇到同样的问题并且可以给我一个解决它的技巧?

我的TableView:

ListModel {
id: filesList
ListElement {
    name:""
    dir:""
    type:""
    status""
}
 }

 TableView {
id: filesTable
TableViewColumn {
    id:nameColumn
    role: "name"
    title: "Name"
    width: dropFiles.width*.2
}
TableViewColumn {
    id:dirColumn
    role: "dir"
    title: "Path"
    width: dropFiles.width*.8
}
TableViewColumn {
    id:typeColumn
    visible: false;
    role: "type"
    title: "Type"
    width: dropFiles.width*.2
}
TableViewColumn {
    id:statusColumn
    visible: false;
    role: "status"
    title: "Status"
    width: dropFiles.width*.3
}
rowDelegate: Rectangle {
    anchors {
        left: parent.left
        right: parent.right
        verticalCenter: parent.verticalCenter
    }
    height: parent.height
    MouseArea {
        anchors.fill: parent
        acceptedButtons: Qt.RightButton
        onClicked: {
            if (mouse.button == Qt.RightButton)
            {
                if(styleData.selected){
                    rowContextMenu.popup();
                }
                else {
                    filesTable.selection.deselect(0, filesTable.rowCount-1);
                    filesTable.selection.select(styleData.row);
                    rowContextMenu.popup();
                }
            }
        }
    }
}
anchors.fill: parent
selectionMode: SelectionMode.ExtendedSelection
model: filesList
 }

更新我的TableView的JS函数:

function displayParsingStatus(){
    for(var i= 0; i<newModel.files.length; ++i){
        switch(newModel.files[i].status){
            case 0:
                filesList.get(i).status="<font color=\"green\">Success</font>";
                break;
            case 1:
                var status ="";
                status += "<ul>";
                for(var j=0; j < newModel.files[i].warnings.length;j++){
                    status += "<li><font color=\"orange\">Warning: " + newModel.files[i].warnings[j] + "</font></li>";
                }
                status += "</ul>";
                filesList.get(i).status=status;
                break;
            case 2:
                filesList.get(i).status="<font color=\"red\"><b>Error: " + newModel.files[i].error + "</b></font>";
                break;
            case 3:
                filesList.get(i).status="Ignored";
                break;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您已经在QML委托元素上设置了高度:

height: parent.height

它将高度绑定到父高度。 如果使用表达式设置高度,则每次表达式的任何元素发生更改时,都会触发(并重新评估)。

这就是QML属性有NOTIFY信号的原因。

因此,如果要将高度绑定到其他元素,只需将其指定给height属性。

我没有尝试过,但是孩子们可能正是你们所寻找的:http://doc.qt.io/qt-5/qml-qtquick-item.html#childrenRect.height-prop

您还可以使用三元运算符为属性赋值,即:

height: (model.get(styleData.row)[styleData.role] === 0)?30:100
相关问题