-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSampleModel.cpp
106 lines (85 loc) · 2.28 KB
/
SampleModel.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
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
#include "global.hpp"
#include "SampleModel.hpp"
#include "moc_SampleModel.cpp"
////////////////////////////////////////////////////////////////////////////////
SampleModel::SampleModel()
{
for (size_t i = 0; i < 1024; i++)
masters[i] = QString("Master%1").arg(i);
dataUpdater.start(1000);
connect(&dataUpdater, SIGNAL(timeout()), this, SLOT(onDataUpdate()));
connect(&dataChangedNofifier, SIGNAL(timeout()), this, SLOT(onDataChangedNotify()));
}
SampleModel::~SampleModel()
{
}
QModelIndex SampleModel::index(int row, int column, const QModelIndex &parent) const
{
printf("index %d %d\n", row, column);
if (!parent.isValid()) {
if (column == SampleModel::ColumnActions)
return createIndex(row, 0, row);
}
return QModelIndex();
}
QModelIndex SampleModel::parent(const QModelIndex &child) const
{
return QModelIndex();
}
int SampleModel::rowCount(const QModelIndex &parent) const
{
if (!parent.isValid())
return masters.count();
return 0;
}
int SampleModel::columnCount(const QModelIndex &parent) const
{
return 1;
}
QVariant SampleModel::data(const QModelIndex &index, int role) const
{
if (index.parent().isValid() == false) {
if (role == Qt::DisplayRole)
return QVariant(masters[index.row()]);
}
return QVariant();
}
bool SampleModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.parent().isValid() == false) {
if (role == Qt::DisplayRole) {
masters[index.row()] = value.toString();
if (dataChangedNofifier.isActive() == false)
dataChangedNofifier.start(1000);
return true;
}
}
return false;
}
void SampleModel::onDataUpdate()
{
for (int i = 0; i < masters.count(); i++) {
QModelIndex idx = index(i, 0, QModelIndex());
setData(idx, QString("Master%1 -- %2").arg(i).arg(QTime::currentTime().toString()), Qt::DisplayRole);
}
}
void SampleModel::onDataChangedNotify()
{
emit dataChanged(QModelIndex(), QModelIndex());
}
////////////////////////////////////////////////////////////////////////////////
SampleModelView::SampleModelView()
{
model = new SampleModel;
list = new QListView;
list->setModel(model);
tree = new QTreeView;
tree->setModel(model);
QSplitter *splitter = new QSplitter;
splitter->addWidget(list);
splitter->addWidget(tree);
setCentralWidget(splitter);
}
SampleModelView::~SampleModelView()
{
}