How to create a tableview(Qt5.12)with model as listModel that can host variable number of columns?

故事扮演 提交于 2020-01-17 16:29:20

问题


I am creating a Table with multiple rows and multiple columns.How can I use Qml TableView to create a table with multiple rows and columns?

I tried with an older implementation of tableview but now want to create the same using the new tableview provided in Qt 5.12.Below is the example code of my older implementation

QtObject{
    id:internals
    property int rows:0
    property int col:0
    property int colwidth:0
    property var columnName:[]
}    
ListModel{
        id: libModel
    }
TableView{
    id:tblview
    height:parent.height
    width: parent.width
    model: libModel

    style:TableViewStyle{
        itemDelegate:
            Rectangle {
            border.width: 1
            border.color: 'light grey'
            anchors.rightMargin: 1
            Text {
                id: textItem
                anchors.fill: parent
                verticalAlignment: Text.AlignVCenter
                horizontalAlignment: styleData.textAlignment
                anchors.leftMargin: 12
                text: styleData.value
                elide: Text.ElideRight
                color: textColor
                renderType: Text.NativeRendering
            }
        }
    }

    resources: {
        var temp =[]
        console.log("Column Cout"+internals.col)
        for(var i=0; i<internals.col;i++)
        {
            console.log("Creating a column")
            temp.push(columnComponent.createObject(tblview,{"role":internals.columnName[i],
                                                       "title":internals.columnName[i]
                                                   }))
        }
        //  var objct = temp[temp.length-1]
        // objct.width = tblview.width - ((internals.col -1)*internals.colwidth)
        return temp
    }
    Component{
        id:columnComponent
        TableViewColumn{width: internals.colwidth}
    }

回答1:


I would recommend using a C++ model derived from QAbstractTableModel, as shown in the example.

For delegates, use DelegateChooser and DelegateChoice.

Unfortunately the documentation regarding TableView and DelegateChooser still needs to be improved:

  • QTBUG-73964
  • QTBUG-74965

Until that is added, I would recommend taking a look at the storagemodel manual test. Quoting the delegate code:

TableView {
    id: table
    anchors.fill: parent
    anchors.margins: 10
    clip: true
    model: StorageModel { }
    columnSpacing: 1
    rowSpacing: 1
    delegate: DelegateChooser {
        role: "type"
        DelegateChoice {
            roleValue: "Value"
            delegate: Rectangle {
                color: "tomato"
                implicitWidth: Math.max(100, label.implicitWidth + 8)
                implicitHeight: label.implicitHeight + 4

                Rectangle {
                    x: parent.width - width
                    width: value * parent.width / valueMax
                    height: parent.height
                    color: "white"
                }

                Text {
                    id: label
                    anchors.baseline: parent.bottom
                    anchors.baselineOffset: -4
                    anchors.left: parent.left
                    anchors.leftMargin: 4
                    text: valueDisplay + " of " + valueMaxDisplay + " " + heading
                }
            }
        }
        DelegateChoice {
            roleValue: "Flag"
            // We could use a checkbox here but that would be another component (e.g. from Controls)
            delegate: Rectangle {
                implicitWidth: checkBox.implicitWidth + 8
                implicitHeight: checkBox.implicitHeight + 4
                Text {
                    id: checkBox
                    anchors.baseline: parent.bottom
                    anchors.baselineOffset: -4
                    anchors.left: parent.left
                    anchors.leftMargin: 4
                    text: (checkState ? "☑ " : "☐ ") + heading
                }
            }
        }
        DelegateChoice {
            // roleValue: "String" // default delegate
            delegate: Rectangle {
                implicitWidth: stringLabel.implicitWidth + 8
                implicitHeight: stringLabel.implicitHeight + 4
                Text {
                    id: stringLabel
                    anchors.baseline: parent.bottom
                    anchors.baselineOffset: -4
                    anchors.left: parent.left
                    anchors.leftMargin: 4
                    text: display
                }
            }
        }
    }


来源:https://stackoverflow.com/questions/55508818/how-to-create-a-tableviewqt5-12with-model-as-listmodel-that-can-host-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!