Qt 5.12 Делегат заголовка TableView - PullRequest
0 голосов
/ 15 января 2019

Я создал это табличное представление в Qt 5.12 с новым TableView, но я не знаю, как создать для него заголовок, я прочитал документацию, для него также нет заголовка. Вот несколько скриншотов:

Screenshot

так как я могу добавить headerDelegate для 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
                    }
                }

            }

        }
    }
}

и TableViewColumn не работает

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

я пытаюсь установить его заголовок из c ++, но sth не в порядке с моим кодом

class TableHelper : public QObject
{
    Q_OBJECT

public:

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

основной:

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

qml:

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

У меня есть точка останова в коде C ++, но она никогда не запускается.

1 Ответ

0 голосов
/ 23 апреля 2019

В этом репозитории github показано, как встроить собственные заголовки в 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
          }
      }
  }
...