-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataModel.cpp
More file actions
125 lines (97 loc) · 2.37 KB
/
DataModel.cpp
File metadata and controls
125 lines (97 loc) · 2.37 KB
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
#include "DataModel.h"
#include <QAbstractItemModel>
#include "DataEntry.h"
#include <QDateTime>
DataModel::DataModel(QObject* parent)
: QAbstractTableModel(parent)
{}
DataModel::~DataModel()
{}
int DataModel::rowCount(const QModelIndex & parent) const
{
return m_datas.size();
}
bool DataModel::insertRows(int row, int count, const QModelIndex& parent)
{
beginInsertRows(parent, row, row + count - 1);
endInsertRows();
return true;
}
bool DataModel::removeRows(int row, int count, const QModelIndex& parent)
{
beginRemoveRows(parent, row, row + count - 1);
endRemoveRows();
return true;
}
void DataModel::beginInsertRows(const QModelIndex& parent, int first, int last)
{
QAbstractTableModel::beginInsertRows(parent, first, last);
}
void DataModel::endInsertRows()
{
QAbstractTableModel::endInsertRows();
}
int DataModel::columnCount(const QModelIndex& parent) const
{
return 3;
}
QVariant DataModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid() || role != Qt::DisplayRole) {
return QVariant();
}
const DataEntry& entry = m_datas.at(index.row());
switch (index.column())
{
case 0: return entry.getId();
case 1:
return QDateTime::fromMSecsSinceEpoch(entry.getTime()).toString("yyyy-MM-dd hh:mm:ss.zzz");
case 2: return entry.getValue();
default: return QVariant();
}
}
void DataModel::addData(DataEntry dataEntry)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_datas.append(dataEntry);
endInsertRows();
}
QVariant DataModel::headerData(int section, Qt::Orientation orientation, int role) const
{
//complete function,header include three column that id, time,value
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
{
switch (section)
{
case 0: return "ID";
case 1: return "Time";
case 2: return "Value";
default: return QVariant();
}
}
return QVariant();
}
void DataModel::clear()
{
if (rowCount() == 0)
{
return;
}
beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
m_datas.clear();
endRemoveRows();
}
void DataModel::addDatas(const QList<DataEntry>& dataEntries)
{
if (dataEntries.isEmpty()) {
return; // 如果列表为空,直接返回
}
// 开始插入行
beginInsertRows(QModelIndex(), rowCount(), rowCount() + dataEntries.size() - 1);
// 批量添加数据
for (const DataEntry& entry : dataEntries) {
m_datas.append(entry); // 将每个条目添加到 m_datas
}
// 结束插入行
endInsertRows();
}