-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmymodel.cpp
More file actions
75 lines (61 loc) · 1.65 KB
/
mymodel.cpp
File metadata and controls
75 lines (61 loc) · 1.65 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
#include "mymodel.h"
#include <QDebug>
#include <iostream>
MyModel::MyModel(QObject * parent)
: QAbstractTableModel{parent}
{
}
int MyModel::rowCount(const QModelIndex &) const
{
return my_objects_.count();
}
int MyModel::columnCount(const QModelIndex &) const {
return 4;
}
QVariant MyModel::data(const QModelIndex &index, int role) const {
const auto row = index.row();
if(row == -1) {
return {};
}
const auto my_object = my_objects_[row];
if(role == Qt::DisplayRole) {
switch (index.column()) {
case 0: return my_object.id;
case 1: return my_object.a;
case 2: return my_object.b;
case 3: return my_object.c;
default: return {};
};
}
return {};
}
QVariant MyModel::headerData(int section, Qt::Orientation orientation, int role) const {
if (orientation != Qt::Horizontal || role != Qt::DisplayRole) {
return {};
}
switch (section) {
case 0: return "id";
case 1: return "a";
case 2: return "b";
case 3: return "c";
default: return {};
}
}
void MyModel::add(const MyObject &my_object)
{
beginInsertRows({}, my_objects_.count(), my_objects_.count());
my_objects_.push_back(my_object);
endInsertRows();
}
void MyModel::edit(const MyObject& my_object) {
const auto id = my_object.id;
for(auto i = 0u; i < my_objects_.size(); ++i) {
if(my_objects_[i].id == id) {
my_objects_[i].a = my_object.a;
my_objects_[i].b = my_object.b;
my_objects_[i].c = my_object.c;
emit dataChanged(index(i, 1), index(i, 3));
break;
}
}
}