-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathListModel.cpp
44 lines (37 loc) · 1.12 KB
/
ListModel.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
#include "ListModel.h"
#include "Translator.h"
ListModel::ListModel(QObject *parent)
: QAbstractListModel(parent)
{
modelData = QStringList{QObject::tr("第一项"),QObject::tr("第二项"),QObject::tr("第三项")};
//connect(Translator::getInstance(), &Translator::languageChanged, this,[this]{
// //emit dataChanged(index(0,0), index(rowCount()-1,0));
// beginResetModel();
// modelData = QStringList{QObject::tr("第一项"),QObject::tr("第二项"),QObject::tr("第三项")};
// endResetModel();
//});
}
int ListModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return modelData.size();
}
QVariant ListModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if(role == Qt::UserRole){
return modelData.at(index.row());
}else if(role == Qt::UserRole+1){
return tr("翻译");
}
return QVariant();
}
QHash<int, QByteArray> ListModel::roleNames() const
{
return QHash<int, QByteArray>{
{Qt::UserRole, "txt"},
{Qt::UserRole+1, "trans"}
};
}