Added support for editable grid cells.

print
Josef Rokos 9 years ago
parent 2ed7d7077b
commit 9db007b761

@ -83,6 +83,10 @@ public:
return Qt::AlignLeft; return Qt::AlignLeft;
} }
if (role == Qt::EditRole)
{
return dispData;
}
return QVariant::Invalid; return QVariant::Invalid;
} }
@ -208,6 +212,22 @@ private:
QList<QSharedPointer<T> > m_fullList; QList<QSharedPointer<T> > m_fullList;
bool filtered; bool filtered;
// QAbstractItemModel interface
public:
virtual bool setData(const QModelIndex &index, const QVariant &value, int role) override
{
if (role == Qt::EditRole)
{
QSharedPointer<T> 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 #endif // ODBTABLEMODEL_H

@ -16,3 +16,18 @@ void ITableModel::restore()
handleRestore(); 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<int> cols)
{
m_editableCols = cols;
}

@ -3,6 +3,7 @@
#include <QString> #include <QString>
#include <QAbstractTableModel> #include <QAbstractTableModel>
#include <QList>
#include "core_global.h" #include "core_global.h"
@ -19,6 +20,17 @@ protected:
public slots: public slots:
void filter(const QString &filter); void filter(const QString &filter);
void restore(); void restore();
signals:
void editCompleted();
// QAbstractItemModel interface
public:
virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
void setEditableCols(const QList<int> cols);
private:
QList<int> m_editableCols;
}; };
#endif // ITABLEMODEL_H #endif // ITABLEMODEL_H

@ -14,6 +14,7 @@ ShopForm::ShopForm(QWidget *parent) :
ShopService srv; ShopService srv;
m_voucher = srv.createVoucher(); m_voucher = srv.createVoucher();
m_itemsModel = new AutoTableModel<VoucherItem>(this); m_itemsModel = new AutoTableModel<VoucherItem>(this);
m_itemsModel->setEditableCols(QList<int>() << 1);
ui->actualReceipt->setModel(m_itemsModel); ui->actualReceipt->setModel(m_itemsModel);
} }

Loading…
Cancel
Save