I've been working on this for days now and done a lot of searching, and while I've piece together some code, I can't get it to do what I want. I'm trying to create a simple table editor, but I can't seem to get the program to handle nulls properly. Ignoring null dates for the time being, which seems to be a separate issue altogether.

I noticed when I was scrolling through the records that any null numeric fields would just display the last non-null value that they previously displayed. Also, if the field was empty, the database would complain about a '' being written to a numeric field. I was able to fix both these problems by adding a custom delegator, but that caused some new problems.

Qt Code:
  1. void NullDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
  2. {
  3. if (editor->inherits("QLineEdit"))
  4. {
  5. QLineEdit *e = static_cast<QLineEdit*>(editor);
  6. QVariant editorValue;
  7. if (!(e->text().isEmpty())) {
  8. editorValue = e->text();
  9. }
  10. model->setData(index, editorValue);
  11. }
  12. else {
  13. QItemDelegate::setModelData(editor, model, index);
  14. }
  15. }
  17. void NullDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  18. {
  19. if (editor->inherits("QLineEdit"))
  20. {
  21. QLineEdit *e = static_cast<QLineEdit*>(editor);
  23. if (index.data().isNull()) {
  24. e->clear();
  25. }
  26. else {
  27. e->setText(index.data(Qt::DisplayRole).toString());
  28. }
  29. }
  30. else {
  31. QItemDelegate::setEditorData(editor, index);
  32. }
  33. }
  35. NullDelegate::NullDelegate(QObject *parent)
  36. : QItemDelegate(parent)
  37. {
  38. }
To copy to clipboard, switch view to plain text mode 

Problem #1: This affects strings and numerics and I'm not sure how to distinguish between the two. I'm OK, with storing '' for strings, but I'd prefer to store nulls for numerics when the text is ''.

Problem #2: If I try and set the value back to '', it just re-displays the last non-null value.

It seems to me that the setData is not accepting nulls, but maybe I'm doing something wrong here.

Any help would be greatly appreciated. I'm fairly new to both Qt and forums, so let me know what other information I need to provide.