Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Additional columns and customs delegate for TableView based on QAbstractTableModel.
Qt 6.11 is out! See what's new in the release blog

Additional columns and customs delegate for TableView based on QAbstractTableModel.

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jefdebusser
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #2

      https://doc.qt.io/qt-5/qml-qt-labs-qmlmodels-tablemodel.html#using-delegatechooser-with-tablemodel

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved