-
Notifications
You must be signed in to change notification settings - Fork 12
/
checkboxstandarditem.cc
81 lines (79 loc) · 3.47 KB
/
checkboxstandarditem.cc
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
#include "checkboxstandarditem.hpp"
CheckBoxStandardItem::CheckBoxStandardItem(const QString &text)
: QStandardItem(text)
{
setCheckable(true);
}
void CheckBoxStandardItem::setData(const QVariant &value, int role)
{
if (role == Qt::CheckStateRole) { //针对复选框变动做操作
Qt::CheckState check_state = (Qt::CheckState) value.toInt();
switch (check_state) {
case Qt::Unchecked: //取消
for (int i = 0, num = rowCount(); i < num; i++) {
child(i)->setData(Qt::Unchecked, Qt::CheckStateRole);
}
//修改内容-必须先修改自己再通知父节点
QStandardItem::setData(value, role);
//通知父节点,我取消了选择,直接告诉父节点半选即可
if (parent()) {
parent()->setData(Qt::PartiallyChecked, role);
}
return; //此事件已完成直接return
case Qt::PartiallyChecked: { //半选
Qt::CheckState current_state = checkState(); //当前状态
int checked_num = 0; //被选择的数量
int unchecked_num = 0; //未选择的数量
bool is_partially = false;
Qt::CheckState child_state;
int m_rowCount = rowCount();
//遍历所有子节点
for (int i = 0; i < m_rowCount; i++) {
child_state = child(i)->checkState();
//子节点半选,则直接半选
switch (child_state) {
case Qt::PartiallyChecked: is_partially = true; break;
case Qt::Unchecked: unchecked_num++; break;
case Qt::Checked: checked_num++; break;
default: checked_num++; break;
}
}
//根据子节点状态确定当前节点应该设置的状态
Qt::CheckState now_state;
if (is_partially) {
now_state = Qt::PartiallyChecked;
} else if (checked_num == m_rowCount) {
now_state = Qt::Checked;
} else if (unchecked_num == m_rowCount) {
now_state = Qt::Unchecked;
} else {
now_state = Qt::PartiallyChecked;
}
//修改状态并通知父节点
if (current_state != now_state) {
//修改内容-必须先修改自己再通知父节点
QStandardItem::setData(now_state, role);
//通知父节点,我的状态更改,也就是父节点进入半选
if (parent()) {
parent()->setData(Qt::PartiallyChecked, role);
}
}
}
return; //此事件已完成直接return
case Qt::Checked: //全选
for (int i = 0, num = rowCount(); i < num; i++) {
child(i)->setData(Qt::Checked, Qt::CheckStateRole);
}
//修改内容-必须先修改自己再通知父节点
QStandardItem::setData(value, role);
//通知父节点,我被选了,也就是父节点进入半选
if (parent()) {
parent()->setData(Qt::PartiallyChecked, role);
}
return; //此事件已完成直接return
default: //如果出现此情况就是错了,可以加错误处理
break;
}
}
QStandardItem::setData(value, role);
}