ListDir.h#ifndef LISTDIR_H #define LISTDIR_H #include <QAbstractListModel> #include <QVariant> #include <QStringList> class ListDir : public QAbstractListModel{ Q_OBJECTpublic: ListDir(QStringList &strings, QObject * parent = 0) : QAbstractListModel(parent),stringList(strings) {} int rowCount(const QModelIndex &index) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;private: QStringList stringList;};#endif
ListDir.cpp#include <QtGui> #include <QDir> #include "ListDir.h" int ListDir::rowCount(const QModelIndex &parent = QModelIndex()) const{ return stringList.count();}QVariant ListDir::data(const QModelIndex &index, int role) const{ if (!index.isValid()) return QVariant(); if (!index.row() >= stringList.size()) return QVariant(); if (role == Qt::DisplayRole) stringList.at(index.row()); else return QVariant();}
const char * path = "/"; QDir dir(path); dir.setFilter(QDir::Dirs); QStringList fileList = dir.entryList(); model = new ListDir(fileList,this); QListView *dirList = new QListView(this); dirList->setModel(model);
void ListDir::setupMyList(){ beginInsertRows(QModelIndex(), 0, 4); stringList.insert(0,"Test_1"); stringList.insert(1,"Test_2"); stringList.insert(2,"Test_3"); stringList.insert(3,"Test_4"); endInsertRows();}
model = new ListDir(this); model->setupMyList(); QListView *mydir = new QListView(this); mydir->setModel(model);
class CAELogModel : public QAbstractTableModel{public: CAELogModel(QObject* parent); ~CAELogModel(); int rowCount(const QModelIndex &parent = QModelIndex()) const; int columnCount(const QModelIndex &parent = QModelIndex()) const; QVariant headerData(int section, Qt::Orientation orientation, int role) const; Qt::ItemFlags flags(const QModelIndex &index) const; QVariant data(const QModelIndex &index, int role) const;private: QVector<CLogEvent> m_events;} ....QVariant CAELogModel::data(const QModelIndex &index, int role) const{ Q_ASSERT(index.row() < m_events.count()); if(!index.isValid()) return QVariant(); int row = index.row(); switch(role) { case Qt::DisplayRole: { const CLogEvent& logEvent = m_events.at(row); switch(index.column()) { case 0: return logEvent.hostName(); case 1: return logEvent.timestamp().toString("dd.MM.yyyy hh:mm:ss"); case 2: return logEvent.operation(); case 3: return logEvent.description(); case 4: return logEvent.object_uid().toString(); case 5: return logEvent.advanced_details(); } } break;return QVariant();}int CAELogModel::rowCount(const QModelIndex &parent) const{ Q_UNUSED(parent); return m_events.count();}int CAELogModel::columnCount(const QModelIndex &parent) const{ Q_UNUSED(parent); return 6;}
QVariant ListDir::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); if (index.row() >= stringList.size()) return QVariant(); switch (role) { case Qt::DisplayRole: return stringList.at(index.row()); break; default:; } return QVariant(); }
if (role == Qt::DisplayRole) stringList.at(index.row()); else return QVariant();
if (role == Qt::DisplayRole) return stringList.at(index.row()); else return QVariant();