diff --git a/core/autotablemodel.h b/core/autotablemodel.h index 047d0f0..4858ce0 100644 --- a/core/autotablemodel.h +++ b/core/autotablemodel.h @@ -83,6 +83,10 @@ public: return Qt::AlignLeft; } + if (role == Qt::EditRole) + { + return dispData; + } return QVariant::Invalid; } @@ -208,6 +212,22 @@ private: QList > m_fullList; bool filtered; + + // QAbstractItemModel interface +public: + virtual bool setData(const QModelIndex &index, const QVariant &value, int role) override + { + if (role == Qt::EditRole) + { + QSharedPointer entity = m_list.at(index.row()); + QObject *rawEntity = (QObject*)entity.data(); + + rawEntity->setProperty(rawEntity->metaObject()->property(index.column() + 1).name(), value); + } + + emit editCompleted(); + return true; + } }; #endif // ODBTABLEMODEL_H diff --git a/core/itablemodel.cpp b/core/itablemodel.cpp index bf408f4..dfde394 100644 --- a/core/itablemodel.cpp +++ b/core/itablemodel.cpp @@ -16,3 +16,18 @@ void ITableModel::restore() handleRestore(); } +Qt::ItemFlags ITableModel::flags(const QModelIndex &index) const +{ + if (m_editableCols.contains(index.column())) + { + return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled; + } + + return QAbstractTableModel::flags(index); +} + +void ITableModel::setEditableCols(const QList cols) +{ + m_editableCols = cols; +} + diff --git a/core/itablemodel.h b/core/itablemodel.h index f952daf..e30780f 100644 --- a/core/itablemodel.h +++ b/core/itablemodel.h @@ -3,6 +3,7 @@ #include #include +#include #include "core_global.h" @@ -19,6 +20,17 @@ protected: public slots: void filter(const QString &filter); void restore(); + +signals: + void editCompleted(); + + // QAbstractItemModel interface +public: + virtual Qt::ItemFlags flags(const QModelIndex &index) const override; + void setEditableCols(const QList cols); + +private: + QList m_editableCols; }; #endif // ITABLEMODEL_H diff --git a/shop/shopform.cpp b/shop/shopform.cpp index 56d466f..5072dfb 100644 --- a/shop/shopform.cpp +++ b/shop/shopform.cpp @@ -14,6 +14,7 @@ ShopForm::ShopForm(QWidget *parent) : ShopService srv; m_voucher = srv.createVoucher(); m_itemsModel = new AutoTableModel(this); + m_itemsModel->setEditableCols(QList() << 1); ui->actualReceipt->setModel(m_itemsModel); }