Added a ModelItem class to hold the data for each row.
This commit is contained in:
@ -1,11 +1,42 @@
|
||||
#include "tablemodel.h"
|
||||
|
||||
#include "modelitem.h"
|
||||
|
||||
enum UserRoles {
|
||||
NameRole = Qt::UserRole + 1,
|
||||
DescriptionRole,
|
||||
InfoRole,
|
||||
AmountRole,
|
||||
FactorRole,
|
||||
FactoredAmountRole
|
||||
};
|
||||
|
||||
TableModel::TableModel(QObject* parent)
|
||||
: QAbstractTableModel{parent} {}
|
||||
: QAbstractTableModel{parent} {
|
||||
for (int row = 0; row < 23; ++row) {
|
||||
QHash<int, QVariant> values;
|
||||
values[NameRole] = QString("Item %1").arg(row);
|
||||
values[DescriptionRole] = QString("This is item %1").arg(row);
|
||||
values[InfoRole] = QString("Info of item %1").arg(row);
|
||||
values[AmountRole] = row;
|
||||
values[FactorRole] = row * 1.1;
|
||||
|
||||
int TableModel::rowCount(const QModelIndex& parent) const { return 5; }
|
||||
shared_ptr<ModelItem> item = make_unique<ModelItem>(values);
|
||||
m_items.append(std::move(item));
|
||||
}
|
||||
}
|
||||
|
||||
int TableModel::columnCount(const QModelIndex& parent) const { return 5; }
|
||||
Qt::ItemFlags TableModel::flags(const QModelIndex& index) const {
|
||||
// return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
|
||||
return QAbstractTableModel::flags(index);
|
||||
}
|
||||
|
||||
int TableModel::rowCount(const QModelIndex& parent) const { return m_items.size(); }
|
||||
|
||||
int TableModel::columnCount(const QModelIndex& parent) const {
|
||||
// TODO read from amount of header names (when available)
|
||||
return 5;
|
||||
}
|
||||
|
||||
QVariant TableModel::data(const QModelIndex& index, int role) const {
|
||||
const int row = index.row();
|
||||
@ -14,13 +45,14 @@ QVariant TableModel::data(const QModelIndex& index, int role) const {
|
||||
if (!index.isValid()) {
|
||||
return QVariant();
|
||||
}
|
||||
if (row >= 5 || column >= 5) {
|
||||
if (row >= rowCount(QModelIndex()) || column >= columnCount(QModelIndex())) {
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
int roleForColumn = getRoleForColumn(column);
|
||||
switch (role) {
|
||||
case Qt::DisplayRole:
|
||||
return QString("Data %1/%2").arg(row).arg(column);
|
||||
return m_items.at(row)->data(roleForColumn);
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
@ -36,3 +68,48 @@ QVariant TableModel::headerData(int section, Qt::Orientation orientation, int ro
|
||||
}
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
bool TableModel::setData(const QModelIndex& index, const QVariant& value, int role) {
|
||||
if (role == Qt::EditRole) {
|
||||
if (!checkIndex(index)) {
|
||||
return false;
|
||||
}
|
||||
// save value from editor to member m_gridData
|
||||
// m_gridData[index.row()][index.column()] = value.toString();
|
||||
// // for presentation purposes only: build and emit a joined string
|
||||
// QString result;
|
||||
// for (int row = 0; row < ROWS; row++) {
|
||||
// for (int col = 0; col < COLS; col++) {
|
||||
// result += m_gridData[row][col] + ' ';
|
||||
// }
|
||||
// }
|
||||
// emit editCompleted(result);
|
||||
// return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// bool TableModel::setItemData(const QModelIndex& index, const QMap<int, QVariant>& roles) {}
|
||||
|
||||
int TableModel::getRoleForColumn(const int column) const {
|
||||
switch (column) {
|
||||
case 0:
|
||||
return NameRole;
|
||||
break;
|
||||
case 1:
|
||||
return DescriptionRole;
|
||||
break;
|
||||
case 2:
|
||||
return InfoRole;
|
||||
break;
|
||||
case 3:
|
||||
return AmountRole;
|
||||
break;
|
||||
case 4:
|
||||
return FactorRole;
|
||||
break;
|
||||
default:
|
||||
return NameRole;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user