-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCustomJsonModel.cpp
61 lines (53 loc) · 1.65 KB
/
CustomJsonModel.cpp
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
#include "CustomJsonModel.h"
CustomJsonModel::CustomJsonModel(const QJsonArray &src, QObject *parent)
: QAbstractTableModel(parent)
, m_src(src)
, m_rowCount(src.count())
{
if (m_rowCount > 0) {
for (auto it = m_src.constBegin(); it != m_src.constEnd(); ++it) {
if (it->isObject()) {
m_data.push_back(it->toObject().toVariantMap());
}
}
m_vmapHead = m_data.front();
m_columnCount = m_vmapHead.count();
m_headers = std::make_unique<QString[]>(m_columnCount);
auto keys = m_vmapHead.keys();
int i = 0;
for (const auto &each : keys) {
m_headers[i++] = each;
}
}
}
int CustomJsonModel::columnCount(const QModelIndex &parent) const {
return m_columnCount;
}
QString CustomJsonModel::currencyAt(int offset) const {
return (m_vmapHead.begin() + offset).key();
}
QVariant CustomJsonModel::data(const QModelIndex &index, int role) const {
if (!index.isValid())
return QVariant();
if (role == Qt::TextAlignmentRole) {
return int(Qt::AlignRight | Qt::AlignVCenter);
}
else if (role == Qt::DisplayRole) {
const auto row = m_data.at(index.row());
return row.find(m_headers[index.column()]).value();
}
return QVariant();
}
int CustomJsonModel::rowCount(const QModelIndex &parent) const {
return m_rowCount;
}
QVariant CustomJsonModel::headerData(int section, Qt::Orientation orientation, int role) const {
if (role == Qt::DisplayRole)
{
if (orientation == Qt::Horizontal)
{
return m_headers[section];
}
}
return QVariant();
}