worksmodel.h:
C++ (Qt)
#ifndef WORKSMODEL_H
#define WORKSMODEL_H
#include <QAbstractTableModel>
#include <QList>
#include "work.h"
class Worksmodel : public QAbstractTableModel
{
public:
Worksmodel(QList<Work> *worklist, QObject *parent = 0);
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation,int role) const;
QList<Work> *worklist;
};
#endif
worksmodel.cpp:
C++ (Qt)
#include "worksmodel.h"
#include <QList>
Worksmodel::Worksmodel(QList<Work> *worklist, QObject *parent) : QAbstractTableModel(parent)
{
this->worklist = worklist;
}
int Worksmodel::rowCount(const QModelIndex & ) const
{
return worklist->count();
}
int Worksmodel::columnCount(const QModelIndex & ) const
{
return(5);
}
QVariant Worksmodel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::TextAlignmentRole) {
return int(Qt::AlignLeft | Qt::AlignVCenter);
} else if (role == Qt::DisplayRole) {
switch (index.column())
{
case 0: {return worklist->at(index.row()).id; }
case 1: {return worklist->at(index.row()).name; }
case 2: {return worklist->at(index.row()).mh; }
case 3: {return worklist->at(index.row()).md; }
case 4: {
QString after;
if(!worklist->at(index.row()).after.isEmpty())
{
after = QString::number(worklist->at(index.row()).after[0]);
for(int i = 1; i < worklist->at(index.row()).after.count(); ++i)
{
after += ", "+QString::number(worklist->at(index.row()).after[i]);
}
return after;
}
return "0";
}
}
}
return QVariant();
}
QVariant Worksmodel::headerData(int section, Qt::Orientation orientation ,int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
QStringList headers;
headers << "#"
<< "Work's name"
<< "Man-hour"
<< "Man-day"
<< "Do after:";
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
{
return headers[section];
}
return QVariant();
}
Класс work:
C++ (Qt)
class Work{ public:
int id;
QString name;
double mh;
double md;
QList<int> after;
};