forked from y673893131/qmlTableView5_12
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTableViewEx.qml
154 lines (134 loc) · 4.77 KB
/
TableViewEx.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import QtQuick 2.12
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
GridLayout
{
id: root
anchors.fill: parent
property alias model: view.model
property int rowHeight: 35
property alias widths: header.widths
columns: 1
rows: 2
function rowCount(){ return root.model.count }
//header
HeaderViewEx
{
id: header
Layout.fillWidth: true
spacing: 1
model: root.model.headModel
contentX: view.contentX
contentWidth: widthsLen()
interactive: false
color: "#E2EEFB"
property real p: 0
onMoveSection: {
model.layoutChanged()
view.model.layoutChanged()
forceLayout();
view.forceLayout();
contentX = view.contentX
}
onColumnWidthChanged: { view.forceLayout(); view.contentWidth = widthsLen(); }
onJump: {
if(!inOrDe) {horScrollBar.decrease()
// inOrDe ? horScrollBar.increase() : horScrollBar.decrease();//qt bug, increase make view cannot align column with header
model.layoutChanged()
view.forceLayout();}
}
}
//view
TableView
{
id: view
property var hoverIndex: -1
property var currentIndex: -1
Layout.fillWidth: true
Layout.fillHeight: true
columnSpacing: 1
rowSpacing: 1
clip: true
reuseItems: false
contentWidth: header.widthsLen()
columnWidthProvider: header.columnWidthProvider
rowHeightProvider: function (column) { return root.rowHeight }
flickableDirection: Flickable.VerticalFlick
synchronousDrag: true
ScrollBar.horizontal: ScrollBar { id: horScrollBar; active: true; onActiveChanged: active = true;}
ScrollBar.vertical: ScrollBar{ id: scrollBar; minimumSize: 0.1; active: true; onActiveChanged: active = true}
Keys.onUpPressed: upRow()
Keys.onDownPressed: downRow()
Component.onCompleted: { view.forceActiveFocus(); selected.connect(onSelected)}
onContentXChanged: header.contentX = contentX
delegate: Rectangle
{
id: cashierContent
color:
{
view.currentIndex === row ? "#3298FE" :
(view.hoverIndex === row ? "#97CBFF" : (row % 2 ? "#E0EFFD" : "#E7F6FD"))
}
Text {
id: textId
text: view.data(row, header.logicIndexMap[column], Qt.DisplayRole)
anchors.fill: parent
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
font: Global_Object.font
color: view.currentIndex === row ? "white" : "black"
elide: Text.ElideRight
onContentWidthChanged:
{
var i = header.logicIndexMap[column]
if(typeof header.maxContentWith[i] === "undefined" || header.maxContentWith[i] < contentWidth)
header.maxContentWith[i] = contentWidth
}
}
MouseArea
{
id: cashierMouse
anchors.fill: parent
hoverEnabled: true
onClicked:
{
view.forceActiveFocus()
view.currentIndex = row
}
onDoubleClicked:
{
view.selected(row, header.logicIndexMap[column])
}
onEntered: view.hoverIndex = row
onExited: view.hoverIndex = -1
}
}
signal selected(var row,var column)
function onSelected(row, column)
{
console.log("double click:", data(row, column, Qt.DisplayRole) + "(" + row + "," + column + ")")
}
function upRow()
{
currentIndex = currentIndex > 0 ? currentIndex - 1 : currentIndex
if(!isCurrentIndexInView(currentIndex - 1))
{
contentY -= (root.rowHeight + rowSpacing)
if(contentY < 0) contentY = 0
}
console.log("up:" + currentIndex)
}
function downRow()
{
currentIndex = currentIndex < root.rowCount() - 1 ? currentIndex + 1 : currentIndex
if(!isCurrentIndexInView(currentIndex + 1))
contentY += (root.rowHeight + rowSpacing)
console.log("down:" + currentIndex)
}
function isCurrentIndexInView(index)
{
return (index * (root.rowHeight + rowSpacing) >= contentY && index * (root.rowHeight + rowSpacing)<= contentY + height)
}
function data(row, column, role){ return model.data(row, column, role)}
}
}