Hi everybody,

I'm currently busy playing with QAbstractItemModels and QItemDelegates.
This is de situation, I have a column warrenty date ("Geleverd op") and that's a date field in side my model, so I installed a QItemDelegate on a column QItemDelegate::setDelegateForColumn();

EDIT: I changed it to setItemDelegate for testing purpose!
But it doesn't work, it constructs the delegate but he doesn't use the delegate....

this is the code were I set the delegate:
Qt Code:
  1. void OrderAddWindow::setTable()
  2. {
  3. qDebug("OrderAddWindow::setTable(): Loading table and model");
  4.  
  5. QStringList header;
  6. QList<QStringList> articleList;
  7.  
  8. header << "Artikel nr" << "Beschrijving" << "Bewerking" << "Prijs" << "Besteld" << "Geleverd" << "Geleverd op";
  9.  
  10.  
  11. model = new ArticlesAddTableModel( articleList , header);
  12. this->tableArticles->setModel(model);
  13. this->tableArticles->setItemDelegate(new DateDelegate(this->tableArticles));
  14.  
  15. }
To copy to clipboard, switch view to plain text mode 
This is de delegate code:
Qt Code:
  1. #include "dateDelegate.h"
  2. #include <QtGui>
  3.  
  4. DateDelegate::DateDelegate(QObject *parent)
  5. : QItemDelegate(parent)
  6. {
  7. qDebug("DateDelegate::DateDelegate():Constructing delegate");
  8. }
  9.  
  10. QWidget* DateDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex& index)
  11. {
  12. qDebug("DateDelegate::createEditor(): Creating delegate widget");
  13. //create the view for the item
  14. const QAbstractItemModel *model = index.model();
  15.  
  16. if(!model)
  17. QItemDelegate::createEditor(parent, option, index);
  18.  
  19. QDateEdit *edit = new QDateEdit(parent);
  20. edit->setDisplayFormat("dd-MM-yyyy");
  21. return edit;
  22. }
  23.  
  24. void DateDelegate::setEditorData(QWidget *editor, const QModelIndex& index)
  25. {
  26. qDebug("DateDelegate::setEditorDate(): Set data");
  27. QDateEdit *edit = qobject_cast<QDateEdit*>(editor);
  28.  
  29. if(!edit)
  30. QItemDelegate::setEditorData(editor, index);
  31. edit->setDate(QDate::currentDate());
  32.  
  33. }
  34.  
  35. void DateDelegate::setModelData(QWidget *parent, QAbstractItemModel *model, const QModelIndex& index)
  36. {
  37. qDebug("DateDelegate::setModelData(): Set data in model");
  38.  
  39. }
To copy to clipboard, switch view to plain text mode 

and this is de model code and the subclass of the model
Qt Code:
  1. #include "articlesTableModel.h"
  2. #include <QtGui>
  3.  
  4.  
  5. ArticlesTableModel::ArticlesTableModel(const QList<QStringList> &data, const QStringList &header, QObject *parent)
  6. {
  7. qDebug("ArticlesTableModel::ArticlesTableModel(): Constructing the table model");
  8. this->articles = data;
  9. this->header = header;
  10. }
  11.  
  12.  
  13. int ArticlesTableModel::rowCount( const QModelIndex& parent) const
  14. {
  15. Q_UNUSED(parent);
  16. return articles.count();
  17. }
  18.  
  19. int ArticlesTableModel::columnCount( const QModelIndex& parent) const
  20. {
  21. Q_UNUSED(parent);
  22. return header.count();
  23. }
  24.  
  25. QVariant ArticlesTableModel::headerData(int section, Qt::Orientation orientation, int role) const
  26. {
  27. if( Qt::Horizontal == orientation)
  28. {
  29. if(Qt::DisplayRole == role)
  30. {
  31. return header.at(section);
  32. }
  33. }
  34.  
  35. return QAbstractTableModel::headerData(section, orientation, role);
  36. }
  37.  
  38. QVariant ArticlesTableModel::data(const QModelIndex &index, int role) const
  39. {
  40.  
  41. if(!index.isValid())
  42. return QVariant();
  43.  
  44. QStringList record = this->articles.at(index.row());
  45.  
  46. if(role == Qt::DisplayRole || role == Qt::EditRole){
  47.  
  48. return record.at(index.column());
  49.  
  50. }
  51.  
  52. return QVariant();
  53.  
  54. }
  55.  
  56. Qt::ItemFlags ArticlesTableModel::flags(const QModelIndex &index) const
  57. {
  58. if(!index.isValid())
  59. return 0;
  60.  
  61. return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
  62. }
  63.  
  64.  
  65. bool ArticlesTableModel::setData(const QModelIndex& index, const QVariant& value, int role)
  66. {
  67. if(index.isValid() && (role == Qt::DisplayRole || role == Qt::EditRole)){
  68. this->articles[index.row()][index.column()] = value.toString();
  69. emit dataChanged(index, index);
  70. return true;
  71. }
  72.  
  73. return false;
  74. }
  75.  
  76. bool ArticlesTableModel::insertRows(int row, int count, const QModelIndex &parent)
  77. {
  78. Q_UNUSED(parent);
  79. QStringList emptyRecord;
  80. for(int i = 0; i < columnCount(QModelIndex()); i++)
  81. emptyRecord.append(QString());
  82. beginInsertRows(QModelIndex(), row, row+count-1);
  83. for(int i = 0; i < count; i++){
  84. this->articles.insert(row, emptyRecord);
  85. }
  86. endInsertRows();
  87.  
  88. return true;
  89. }
  90.  
  91. bool ArticlesTableModel::removeRows(int row, int count, const QModelIndex &parent)
  92. {
  93. Q_UNUSED(parent);
  94. if(row-count-1 > this->articles.count() -1) return false;
  95. beginRemoveRows(QModelIndex(), row, row+count-1);
  96. for(int i=0; i<count; i++)
  97. this->articles.removeAt(row);
  98. endRemoveRows();
  99. return true;
  100.  
  101. }
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. #include "ArticlesAddTableModel.h"
  2. #include <QtGui>
  3.  
  4.  
  5. ArticlesAddTableModel::ArticlesAddTableModel(const QList<QStringList> &data, const QStringList &header, QObject *parent)
  6. : ArticlesTableModel(data, header, parent)
  7. {
  8. qDebug("ArticlesAddTableModel::ArticlesAddTableModel(): Constructing the add table model");
  9. }
  10.  
  11. QList<QStringList> ArticlesAddTableModel::retreiveData()
  12. {
  13. return this->articles;
  14. }
  15.  
  16.  
  17. void ArticlesAddTableModel::removeRow()
  18. {
  19. qDebug("ArticlesAddTableModel::removeRow(): Remove row from model");
  20. this->removeRows(rowCount(QModelIndex()) - 1, 1, QModelIndex());
  21. }
  22.  
  23. void ArticlesAddTableModel::addRow()
  24. {
  25.  
  26. qDebug("ArticlesAddTableModel::addRow(): Add an empty row to model");
  27.  
  28. insertRows(rowCount(QModelIndex()), 1, QModelIndex());
  29. }
To copy to clipboard, switch view to plain text mode 

Does somebody know how to trigger createEditor() in the QItemDelegate?

Greetings,

Cyberboy