Qt 5.12 TableView Header Delegate

2019-08-04 18:05发布

I've created this table view in Qt 5.12 with new TableView but I don't know how to create header for it, I read documentation there is no headerdelegate for it neither. Here some screenshot:

Screenshot

so how can i add headerDelegate for 5.12 TableView?

import QtQuick 2.12
import QtQuick.Controls 2.12
ApplicationWindow {
    visible: true
    ListModel{
        id:tableModel
        ListElement{
            identifier:1
            name:'value'
            title: 'test'
        }
        ListElement{
            identifier:2
            name:'value2'
            title: 'test2'
        }
    }

    width:1000; height: 500
    //

    TableView {
        id:trans
        LayoutMirroring.enabled: true
        LayoutMirroring.childrenInherit: true
        anchors.fill: parent
        columnSpacing: 1
        rowSpacing: 1
        clip: true
        model: tableModel

        delegate: Rectangle {

            Row{
                Rectangle{
                    implicitWidth: 100
                    implicitHeight: 50
                    Text{
                        text: identifier
                    }
                }
                Rectangle{
                    implicitWidth: 100
                    implicitHeight: 50
                    Text{
                        text:name
                    }
                }

                Rectangle{
                    implicitWidth: 100
                    implicitHeight: 50
                    Text{
                        text:title
                    }
                }

            }

        }
    }
}

and TableViewColumn didn't work

Qc.TableViewColumn{
    title: 'id'
    role: 'identifier'
    width:100
}

i'm trying to set its header from c++ but sth is not ok with my code

class TableHelper : public QObject
{
    Q_OBJECT

public:

    Q_INVOKABLE void setTableHeader(QObject & table){
        // get QTableView and Set QHeaderView 
        qDebug()<< "here";
    }
};

main :

TableHelper th;
engine.rootContext()->setContextProperty("tableHelper",&th);

qml :

Component.onCompleted: {
        tableHelper.setTableHeader(trans)
}

I have a break point in C++ code, but it never runs.

1条回答
相关推荐>>
2楼-- · 2019-08-04 19:12

This github repo shows how to build in your own headers in a QML TableView (Qt 5.12.0):

TableView {
      anchors.fill: parent
      columnSpacing: 1
      rowSpacing: 1
      clip: true

      model: TableModel {}

      delegate: Rectangle {
          implicitWidth: 150
          implicitHeight: 50
          border.color: "black"
          border.width: 2
          color: (heading==true)?"red":"green" // If heading make it red!

          TableView.onPooled: console.log(tabledata + " pooled")
          TableView.onReused: console.log(tabledata + " resused")

          Text {
              text: tabledata
              font.pointSize: 12
              anchors.centerIn: parent
          }
      }
  }
查看更多
登录 后发表回答