-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcustomtablemodel.cpp
More file actions
62 lines (50 loc) · 1.42 KB
/
Copy pathcustomtablemodel.cpp
File metadata and controls
62 lines (50 loc) · 1.42 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
#include "customtablemodel.h"
const int COLUMN_COUNT = 3;
const int ROW_COUNT= 10;
CustomTableModel::CustomTableModel(QObject *parent)
: QAbstractTableModel(parent)
{
for(auto i = 0; i < ROW_COUNT; i++) {
QString prifix =QString("%1").arg(i);
int suffix = 1;
datas.push_back({prifix+ QString("-%1").arg(suffix++), prifix+ QString("-%1").arg(suffix++), prifix+ QString("-%1").arg(suffix++)});
}
}
QVariant CustomTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
static QList<QString> headers = {"first", " second", "third"};
if (orientation == Qt::Vertical)
{
if (role == Qt::DisplayRole){
return QVariant(QString("%1").arg(section) );
}
} else{
if (role == Qt::DisplayRole){
return headers[section];
}
}
return QVariant();
}
int CustomTableModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return ROW_COUNT;
}
int CustomTableModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return COLUMN_COUNT;
}
QVariant CustomTableModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::DisplayRole){
auto row = index.row();
auto col = index.column();
return datas[row][col];
}
return QVariant();
}