I have http://doc.qt.io/qt-4.8/QTableView connected with http://doc.qt.io/qt-4.8/QSqlQueryModel, the first column I inserted it after populating data from database. The first column will be checkbox, checked is the second column have a matched value from a QStringList variable I pass to the delegate. My problem is the delegate never draws http://doc.qt.io/qt-4.8/QCheckBox on the first column.
Here is how I set the delegate:

Qt Code:
  1. QString sqlQuery;
  2. sqlQuery = "SELECT ProCode, ProName, ProPrice FROM Promotions";
  3.  
  4. model->setQuery(sqlQuery);
  5. model->insertColumns(0, 1);
  6.  
  7. ui->promotionsList->setModel(model);
  8.  
  9. QStringList promotions = this->workerMod->promotionsList->text().split(',');
  10.  
  11. PromotionDelegate *checkDelegatePromos = new PromotionDelegate(this);
  12. checkDelegatePromos->setPromotions(promotions);
  13. ui->promotionsList->setItemDelegate(checkDelegatePromos);
To copy to clipboard, switch view to plain text mode 

The delegate I implement:

Qt Code:
  1. QWidget *PromotionDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
  2. {
  3. if(index.isValid() && index.column() == 0)
  4. {
  5. //QMessageBox::critical(0, QString::number(index.row()), "1"); Also, this message never fired up!
  6. QCheckBox *check = new QCheckBox(parent);
  7. check->setCheckState(Qt::Unchecked);
  8. return check;
  9. }
  10. else
  11. return QItemDelegate::createEditor(parent, option, index);
  12. }
  13.  
  14. void PromotionDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  15. {
  16. if(index.isValid() && index.column() == 0)
  17. {
  18. QCheckBox *check = static_cast<QCheckBox *>(editor);
  19. QModelIndex inx = index.model()->index(index.row(), 1);
  20. QString prom = index.model()->data(inx, 0).toString();
  21. for(int i = 0; i< this->m_promotions.length(); ++i)
  22. {
  23. if(this->m_promotions.at(i) == prom)
  24. {
  25. check->setCheckState(Qt::Checked);
  26. break;
  27. }
  28. }
  29. }
  30. else
  31. QItemDelegate::setEditorData(editor, index);
  32. }
  33.  
  34. void PromotionDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
  35. const QModelIndex &index) const
  36. {
  37.  
  38. }
  39.  
  40. void PromotionDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
  41. {
  42. if(index.isValid() && index.column() == 0)
  43. {
  44. editor->setGeometry(option.rect);
  45. }
  46. else
  47. QItemDelegate::updateEditorGeometry(editor, option, index);
  48. }
  49.  
  50. void PromotionDelegate::setPromotions(QStringList &promotions)
  51. {
  52. this->m_promotions = promotions;
  53. }
To copy to clipboard, switch view to plain text mode