Additional columns and customs delegate for TableView based on QAbstractTableModel.



  • I'm using 5.15.2 and Quick Control 2 but I'm willing to use anything really, provided that it looks and performs OK.

    I have a class derived from QAbstractTableModel describing the status of a processing job.
    The data has a few fixed columns (e.g. id, name, begin, end, ...) and an arbitrary number of user defined columns.
    I want:

    • to show the status of the job using an icon: the name of this column is known beforehand
      • display a number of text columns, the number is dependent on the model, and the names are not known beforehand
    • to add an additional column containing a button to trigger an action

    Using QtQuick.Controls 2.15 I can create a TableView and HorizontalHeaderView and display the table without hard coding the column names.

    Does anybody

        TableView {
            id: tableView
            anchors.top: parent.top
            anchors.topMargin: horizontalHeader.height + rowSpacing
            anchors.left: parent.left
            model: liveJobOverview                                                                      // this is my model based on QAbstractTableModel, it doesn't contain any other role than DisplayRole
            rightMargin: 100
            bottomMargin: 100
            columnSpacing: 4
            rowSpacing: 4
            syncDirection: Qt.Vertical | Qt.Horizontal
            implicitWidth: parent.width + columnSpacing
            implicitHeight: parent.height + rowSpacing
            clip: true
    
            delegate: Rectangle {
                implicitWidth: 150
                implicitHeight: 50
                Text {
                    anchors.fill: parent
                    text: model.display
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                }
            }
        }
    
        HorizontalHeaderView {
            id: horizontalHeader
            objectName: "horizontalHeader"
            height: contentHeight
            width: syncView.width
            anchors.top: parent.top
            anchors.left: tableView.left
            syncView: tableView
            clip: true
        }
    

    I have troubles connecting different delegates to different columns because they all use the display role and for example the delegate chooser relies on the role to select the delegate. I also can't figure out how to use the current index/row/column to select a delegate based on that. Similarly I'm not able to add a column to the table which isn't part of the model.

    Does anybody have a working example of a TableView showing all columns of a C++ model and an additional one that isn't in the model? Using different delegates for different columns?

    All have would be more than welcome.




Log in to reply