https://github.com/eyllanesc/stackoverflow/tree/master/questions/55610163
这是参考的完整源代码
个人修改的源代码看附件https://download.csdn.net/download/qq_24890953/11866548
import QtQuick 2.12
import QtQuick.Controls 2.4
import QtQuick.Window 2.11
Window {
readonly property real __headFirstHeight: 25
readonly property real __headSecondeHeight: 38
readonly property real __headWidth: 220
readonly property string __bordColor: "#e0e0e0"
readonly property string __headColor: "#a4cfca"
readonly property string __headColor2: "#a5d0cb"
visible: true
width: 640
height: 480
title: qsTr("Hello World")
color: 'gray'
TableView {
id: tableView
//QML 新出的设置行宽和列宽的方式,如果需要某一行隐藏,只需要将这一行返回值设置为 0 即可
//具体看官方文档
columnWidthProvider: function (column) { return __headWidth; }
rowHeightProvider: function (column) { return 25; }
anchors.fill: parent
leftMargin: rowsHeader.implicitWidth
topMargin: columnsHeader.implicitHeight
model: table_model
ScrollBar.horizontal: ScrollBar{}
clip: true
//用于关闭拖动效果
// boundsBehavior: Flickable.StopAtBounds
delegate: SysTableViewDelegate {
}
//左上角内容
SysTableLeftTopMaskDelegate {
}
//列头部
Row {
id: columnsHeader
y: tableView.contentY
z: 2
Repeater {
model: tableView.columns > 0 ? tableView.columns : 1
SysTableTextColumnsHeaderDelegate{
width: tableView.columnWidthProvider(modelData)
height: 65
color: __bordColor
textInfo: table_model.headerData(modelData, Qt.Horizontal)
}
}
}
//行头部
Column {
id: rowsHeader
x: tableView.contentX
z: 2
Repeater {
model: tableView.rows > 0 ? tableView.rows : 1
Rectangle {
width: 60
height: tableView.rowHeightProvider(modelData)
color: __bordColor
Label {
anchors.fill: parent
anchors {bottomMargin: 2}
anchors.centerIn: parent
text: table_model.headerData(modelData, Qt.Vertical)
color: '#7785a9'
font.pixelSize: 15
padding: 10
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
background: Rectangle { color: "white" }
}
}
}
}
ScrollIndicator.horizontal: ScrollIndicator { }
}
Component.onCompleted: {
}
}