+ KeyItemDelegate(int min, int max, QAbstractItemView* parent = 0)
+ : QItemDelegate(parent), m_min(min), m_max(max) {
+ }
+
+ QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
+ if (index.column() == 1) {
+ QSpinBox *spin = new QSpinBox(parent);
+ connect(spin, SIGNAL(valueChanged(int)), this, SLOT(commitEditorData()));
+ connect(spin, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
+ return spin;
+ } else {
+ return QItemDelegate::createEditor(parent, option, index);
+ }
+ }
+
+
+ void setEditorData(QWidget *editor, const QModelIndex &index) const {
+ if (index.column() == 1) {
+ QSpinBox *spin = qobject_cast< QSpinBox* >(editor);
+ spin->setRange(m_min, m_max);
+ spin->setValue(index.model()->data(index).toInt());
+ } else {
+ QItemDelegate::setEditorData(editor, index);
+ }
+ }
+
+private slots:
+ void commitAndCloseEditor() {
+ QSpinBox *spin = qobject_cast< QSpinBox* >(sender());
+ emit closeEditor(spin);
+ }
+
+ void commitEditorData() {
+ QSpinBox *spin = qobject_cast< QSpinBox* >(sender());
+ emit commitData(spin);
+ }