-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtreemodel.h
More file actions
70 lines (62 loc) · 1.62 KB
/
treemodel.h
File metadata and controls
70 lines (62 loc) · 1.62 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
#ifndef TREEMODEL_H
#define TREEMODEL_H
/*
*
* Example of QAbstractItemModel with TreeView component
* author Konstantin Frumkin
*
* */
#include <QAbstractItemModel>
#include <QVariant>
class Item {
public:
Item() {
index = counter++;
text += QString::number(index);
}
Item * addChild() {
Item * item = new Item;
item->setParent(this);
children.append(item);
return item;
}
Item * childAt(int t_row) const {
return children.at(t_row);
}
int row() const {
const Item * parent = getParent();
int row = parent->children.indexOf(const_cast<Item *>(this));
return row;
}
int numberOfChildren() const {
return children.size();
}
Item * getParent() const;
void setParent(Item *value);
const QString & getText() const {
return text;
}
QList<Item *> &getChildren();
private:
QString text {"index is "};
QList<Item *> children;
Item * parent;
int index;
static int counter;
};
class TreeModel : public QAbstractItemModel
{
Q_OBJECT
public:
TreeModel();
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &) const;
int rowCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
QModelIndex parent(const QModelIndex &index) const;
/// item to be moved after target item
Q_INVOKABLE void moveRow(const QModelIndex& sourceIndex, const QModelIndex& targetIndex);
private:
Item rootItem;
};
#endif // TREEMODEL_H