#include"mylistmodel.h"MyListModel::MyListModel(QObject *parent):QAbstractListModel(parent){
m_data.append(MyData("zhangsan",111));
m_data.append(MyData("lisi",222));
m_data.append(MyData("wangwu",333));
m_data.append(MyData("test test",123123123));}
MyListModel *MyListModel::getInstance(){static MyListModel* obj =new MyListModel;return obj;}//QVariant MyListModel::headerData(int section, Qt::Orientation orientation, int role) const//{// // FIXME: Implement me!//}// 元素的个数 或者说是model的大小/长度intMyListModel::rowCount(const QModelIndex &parent)const{// For list models only the root node (an invalid parent) should return the list's size. For all// other (valid) parents, rowCount() should return 0 so that it does not become a tree model.if(parent.isValid())return0;return m_data.count();// FIXME: Implement me!}
QVariant MyListModel::data(const QModelIndex &index,int role)const{if(!index.isValid())returnQVariant();if(role == MyRoleName::Name){return m_data[index.row()].m_string;}elseif(role == MyRoleName::Value){return m_data[index.row()].m_value;}// FIXME: Implement me!returnQVariant();}
QHash<int, QByteArray>MyListModel::roleNames()const{
QHash<int,QByteArray> roles;
roles.insert(MyRoleName::Name,"name");//字符串是QML端用
roles.insert(MyRoleName::Value,"value");//枚举是CPP来判断return roles;}