| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#ifndef STRINGMODEL_H |
| 5 |
#define STRINGMODEL_H |
| 6 |
|
| 7 |
#include <QtCore/QObject> |
| 8 |
#include <QtCore/QAbstractItemModel> |
| 9 |
#include <QtCore/QDebug> |
| 10 |
|
| 11 |
class StringModel : public QAbstractItemModel |
| 12 |
{ |
| 13 |
Q_OBJECT |
| 14 |
QList<QString> items; |
| 15 |
QHash<int, QByteArray> roles; |
| 16 |
QString name; |
| 17 |
|
| 18 |
public: |
| 19 |
explicit StringModel(const QString& name) : QAbstractItemModel(), name(name) |
| 20 |
{ |
| 21 |
roles.insert(555, "text"); |
| 22 |
} |
| 23 |
|
| 24 |
void drop(int count) |
| 25 |
{ |
| 26 |
beginRemoveRows(QModelIndex(), 0, count-1); |
| 27 |
for (int i=0; i<count; i++) |
| 28 |
items.pop_front(); |
| 29 |
endRemoveRows(); |
| 30 |
} |
| 31 |
|
| 32 |
Q_INVOKABLE void add(QString s) |
| 33 |
{ |
| 34 |
beginInsertRows(QModelIndex(), 0, 0); |
| 35 |
items.push_front(s); |
| 36 |
endInsertRows(); |
| 37 |
} |
| 38 |
|
| 39 |
int rowCount(const QModelIndex &) const override |
| 40 |
{ |
| 41 |
return items.size(); |
| 42 |
} |
| 43 |
|
| 44 |
QHash<int, QByteArray> roleNames() const override |
| 45 |
{ |
| 46 |
return roles; |
| 47 |
} |
| 48 |
|
| 49 |
int columnCount(const QModelIndex &) const override |
| 50 |
{ |
| 51 |
return 1; |
| 52 |
} |
| 53 |
|
| 54 |
bool hasChildren(const QModelIndex &) const override |
| 55 |
{ |
| 56 |
return rowCount(QModelIndex()) > 0; |
| 57 |
} |
| 58 |
|
| 59 |
QModelIndex index(int row, int column, const QModelIndex &parent) const override |
| 60 |
{ |
| 61 |
Q_UNUSED(column); |
| 62 |
if (row >= 0 && row < rowCount(parent)) |
| 63 |
return createIndex(row,0); |
| 64 |
else |
| 65 |
return QModelIndex(); |
| 66 |
} |
| 67 |
|
| 68 |
QModelIndex parent(const QModelIndex &) const override |
| 69 |
{ |
| 70 |
return QModelIndex(); |
| 71 |
} |
| 72 |
|
| 73 |
QVariant data (const QModelIndex & index, int role) const override |
| 74 |
{ |
| 75 |
int row = index.row(); |
| 76 |
if ((row<0) || (row>=items.size())) |
| 77 |
return int(QMetaType::UnknownType); |
| 78 |
|
| 79 |
switch (role) { |
| 80 |
case Qt::DisplayRole: |
| 81 |
case 555: |
| 82 |
return QVariant::fromValue(items.at(row)); |
| 83 |
default: |
| 84 |
return QVariant(); |
| 85 |
} |
| 86 |
} |
| 87 |
}; |
| 88 |
|
| 89 |
#endif |