I've been trying to figure out how Delegates and whatnot work, but am having quite a bit of trouble. I'm fairly certain there's something wrong with my concepts, but after reading the Delegate Classes page and looking through the SpinBox Delegate example, I thought I'd figured out how things work, but clearly haven't.

I'm trying to use delegates to control user input in a QTableWidget so that only numbers are allowed.

The code is as follows.
Qt Code:
  1. class NTableDelegate : public QStyledItemDelegate
  2. {
  3. Q_OBJECT
  4.  
  5. public:
  6. NTableDelegate(QObject* parent);
  7. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
  8. void setEditorData(QWidget *editor, const QModelIndex &index) const;
  9. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
  10. void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
  11. };
  12.  
  13. NTableDelegate::NTableDelegate(QObject *parent) : QStyledItemDelegate(parent)
  14. {
  15. }
  16.  
  17. QWidget* NTableDelegate::createEditor(QWidget* parent,const QStyleOptionViewItem &option,const QModelIndex &index) const
  18. {
  19. QLineEdit* editor = new QLineEdit(parent);
  20. QDoubleValidator* val = new QDoubleValidator(editor);
  21. val->setBottom(0);
  22. val->setNotation(QDoubleValidator::StandardNotation);
  23. editor->setValidator(val);
  24. return editor;
  25. }
  26. void NTableDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  27. {
  28. double value = index.model()->data(index,Qt::EditRole).toDouble();
  29. QLineEdit* line = static_cast<QLineEdit*>(editor);
  30. line->setText(QString().setNum(value));
  31. }
  32. void NTableDelegate::setModelData(QWidget* editor,QAbstractItemModel* model,const QModelIndex &index) const
  33. {
  34. QLineEdit* line = static_cast<QLineEdit*>(editor);
  35. QString value = line->text();
  36. model->setData(index,value);
  37. }
  38. void NTableDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
  39. {
  40. editor->setGeometry(option.rect);
  41. }
To copy to clipboard, switch view to plain text mode 

I basically got the SpinBox Delegate example and tried altering it for my needs, but to no avail. The cells are not visible and not modifiable. If I click the headers, they're selected, which tells me the table is populated, but simply locked away.

However, should I simply comment out the QTableWidget::setItemDelegate() line, all goes back to normal, as shown in the images below. So, my question remains: what have I done wrong?

default.JPGcustom.JPG