qml分类

模拟treeview

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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
RowLayout {
anchors.fill: parent
// anchors.top: header.bottom
spacing: 50
Rectangle {
Layout.preferredWidth: 150
Layout.preferredHeight: 600
ListView{
id:listView
anchors.fill: parent
anchors.top: parent.top
anchors.topMargin:20
spacing: 20
model: ListModel{
id:listModel
ListElement {
bgpId: 2
bgpName: '全部'
level: 0
subNode: []
}
}
delegate: list_delegate
Component.onCompleted: {
//获取分类
get("",
function(result,json){
for(var o in json) {
if(json[o].parentId == 2){
for(var cid in json){
if(json[cid].parentId == json[o].id){
addModelData(json[o].id,json[o].name,json[cid].id,json[cid].name)
}
}
}
}
}
)
//获取全部背景
getInfo(2)
}
}
}
Rectangle {
Layout.alignment: Qt.AlignTop
Layout.preferredWidth: parent.width-200
Layout.preferredHeight: parent.height
ScrollView {
anchors.fill: parent
verticalScrollBarPolicy: Qt.ScrollBarAlwaysOff
Flow {
width: parent.parent.width
rightPadding: 30
spacing: 30
Repeater {
model: contactModel
Image {
sourceSize.height: 100
source: imgurl
Text {
width: parent.width
anchors.top: parent.bottom
horizontalAlignment: Text.AlignHCenter
font.pointSize: 10
text: name
}
}
}
}
}
}
}
ListModel {
id: contactModel
}
Component{
id:list_delegate
Column{
id:objColumn
Component.onCompleted: {
for(var i = 1; i < objColumn.children.length - 1; ++i) {
objColumn.children[i].visible = false
}
}
MouseArea{
width:listView.width
height: objItem.implicitHeight
// enabled: objColumn.children.length > 2
onClicked: {
console.log(bgpId)
//根据分类Id获取背景数据
getInfo(bgpId)
var flag = false
for(var i = 1; i < parent.children.length - 1; ++i) {
flag = parent.children[i].visible;
parent.children[i].visible = !parent.children[i].visible
}
if(!flag){
iconAin.from = 0
iconAin.to = 90
iconAin.start()
}
else{
iconAin.from = 90
iconAin.to = 0
iconAin.start()
}
}
Row{
id:objItem
spacing: 10
leftPadding: 20
Image {
id: icon
width: 10
height: 20
source: "../res/blockicons/Foward.svg"
anchors.verticalCenter: parent.verticalCenter
RotationAnimation{
id:iconAin
target: icon
duration: 100
}
}
Label {
text: bgpName
color:"grey"
anchors.verticalCenter: parent.verticalCenter
}
}
}
Repeater {
model: subNode
delegate: Rectangle{
width: parent.width
height: 40
Column{
anchors{
leftMargin: 20
top: parent.top
}
topPadding: 10
spacing: 2
Label{
horizontalAlignment: Text.AlignHCenter
width: 100
text: model.bgcName
MouseArea {
anchors.fill: parent
onClicked: {
console.log(model.bgcId)
//根据子分类Id获取背景数据
getInfo(model.bgcId)
}
}
}
}
}
}
}
}
//添加
function addModelData(bgpId,bgpName,bgcId,bgcName){
var index = findIndex(bgpId)
if(index === -1){
listModel.append({"bgpId":bgpId,"bgpName":bgpName,"level":0,
"subNode":[{"bgcId":bgcId,"bgcName":bgcName,"level":1,"subNode":[]}]})
}
else{
listModel.get(index).subNode.append({"bgcId":bgcId,"bgcName":bgcName,"level":1,"subNode":[]})
}
}
//查找
function findIndex(id){
for(var i = 0 ; i < listModel.count ; ++i){
if(listModel.get(i).bgpId === id){
return i
}
}
return -1
}