-
Notifications
You must be signed in to change notification settings - Fork 0
/
accountmodel.cpp.autosave
142 lines (122 loc) · 3.54 KB
/
accountmodel.cpp.autosave
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
#include "accountmodel.h"
//#include "data.h"
#include <QDebug>
#include "login_with_lock.h"
AccountModel::AccountModel(QObject *parent)
: QAbstractTableModel(parent)
{
}
int AccountModel::rowCount(const QModelIndex &parent) const//用于确定表格的行数
{
if (parent.isValid())
return 0;
return m_accounts.size();
}
int AccountModel::columnCount(const QModelIndex &parent) const//用于确定表格的列数
{
if (parent.isValid())
return 0;
return 7; // 有7个字段
}
QVariant AccountModel::data(const QModelIndex &index, int role) const//根据index返回对应数据,role是数据显示的格式标志
{
if (!index.isValid())
return QVariant();
if (role == Qt::DisplayRole || role == Qt::EditRole) {
const Account &account = m_accounts[index.row()];
switch (index.column()) {
case 0:
return account.card_ID;
case 1:
return account.username;
case 2:
return account.passward;
case 3:
return account.money;
case 4:
return account.login_failures;
case 5:
return account.phone;
case 6:
return account.isAdministrator;
default:
return QVariant();
}
}
return QVariant();
}
QVariant AccountModel::headerData(int section, Qt::Orientation orientation, int role) const//提供表格的表头
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal) {
switch (section) {
case 0:
return tr("卡号");
case 1:
return tr("用户名");
case 2:
return tr("密码");
case 3:
return tr("余额");
case 4:
return tr("登录失败次数");
case 5:
return tr("手机号");
case 6:
return tr("是否为管理员");
default:
return QVariant();
}
}
return QVariant();
}
Qt::ItemFlags AccountModel::flags(const QModelIndex &index) const//检测index的有效
{
Qt::ItemFlags flags = QAbstractTableModel::flags(index);
if (index.isValid()) {
flags |= Qt::ItemIsEditable;
}
return flags;
}
bool AccountModel::setData(const QModelIndex &index, const QVariant &value, int role)//编辑数据
{
if (!index.isValid() || role != Qt::EditRole)
return false;
Account &account = m_accounts[index.row()];
switch (index.column()) {
case 0:
account.card_ID = value.toInt();
break;
case 1:
account.username = value.toString();
break;
case 2:
account.passward = value.toString();
break;
case 3:
account.money = value.toDouble();
break;
case 4:
account.login_failures = value.toInt();
break;
case 5:
account.phone = value.toString();
break;
case 6:
account.isAdministrator = value.toInt();
break;
default:
return false;
}
//emit dataChanged(index, index);
emit layoutChanged();//发出数据改变的信号,更新表格中数据
emit accountDataChanged(m_accounts);//发出数据改变的信号,更新结构体数组中的数据
return true;
}
void AccountModel::updateData(const QList<Account> &newData)//将更新的数据
{
beginResetModel();
m_accounts = newData;
endResetModel();
}