-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchatrecordsmodel.cpp
69 lines (58 loc) · 1.41 KB
/
chatrecordsmodel.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
#include "chatrecordsmodel.h"
ChatRecordsModel::ChatRecordsModel(QObject *parent) : QAbstractListModel(parent)
{
}
void ChatRecordsModel::pushBack(QString content,bool direction)
{
beginInsertColumns(QModelIndex(),rowCount(),rowCount());
ChatRecordItem item=ChatRecordItem(content,direction);
items.push_back(item);
endInsertRows();
}
void ChatRecordsModel::clear()
{
beginRemoveRows(QModelIndex(),0,items.size());
items.clear();
endRemoveRows();
}
//ChatRecordItem::ChatRecordItem(QString content)
//{
// this->content=content;
//}
ChatRecordItem::ChatRecordItem(QString content, bool direction)
{
this->content=content;
this->direction=direction;
}
QString ChatRecordItem::getContent() const
{
return content;
}
bool ChatRecordItem::getDirection() const
{
return direction;
}
int ChatRecordsModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return this->items.size();
}
QVariant ChatRecordsModel::data(const QModelIndex &index, int role) const
{
int row=index.row();
ChatRecordItem item=items[row];
if(role==Role::role1){
return item.getContent();
}
else if(role==Role::role2){
return item.getDirection();
}
return QVariant();
}
QHash<int, QByteArray> ChatRecordsModel::roleNames() const
{
QHash<int,QByteArray> hash;
hash[Role::role1]="content";
hash[Role::role2]="direction";
return hash;
}