Hi all,

How can i hav different delegates in different columns of a table. for instance, i want spinbox delegate in 2nd column and combobox in 4 column.

i took an example of spinbox delegate from examples directories and tried to do the changes:

Qt Code:
  1. /*
  2.   main.cpp
  3.  
  4.   A simple example that shows how a view can use a custom delegate to edit
  5.   data obtained from a model.
  6. */
  7.  
  8. #include <QApplication>
  9. #include <QHeaderView>
  10. #include <QItemSelectionModel>
  11. #include <QStandardItemModel>
  12. #include <QTableView>
  13. #include <QObject>
  14.  
  15. #include "delegate.h"
  16.  
  17. int main(int argc, char *argv[])
  18. {
  19. QApplication app(argc, argv);
  20.  
  21. QStandardItemModel model(4, 2);
  22. QTableView tableView;
  23. tableView.setModel(&model);
  24.  
  25. SpinBoxDelegate delegate;
  26. ComboBoxDelegate comDel;
  27.  
  28.  
  29. for (int row = 0; row < 4; ++row)
  30. {
  31. for (int column = 0; column < 1; ++column)
  32. {
  33. tableView.setItemDelegate(&comDel);
  34. QModelIndex index = model.index(0, 0, QModelIndex());
  35. }
  36. for (int column = 1; column < 2; ++column)
  37. {
  38. tableView.setItemDelegate(&delegate);
  39. QModelIndex index = model.index(1, 1, QModelIndex());
  40. }
  41. }
  42.  
  43. tableView.setWindowTitle(QObject::tr("Spin Box Delegate"));
  44. tableView.show();
  45.  
  46. return app.exec();
  47. }
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. /*
  2.   delegate.cpp
  3.  
  4.   A delegate that allows the user to change integer values from the model
  5.   using a spin box widget.
  6. */
  7.  
  8. #include <QtGui>
  9.  
  10. #include "delegate.h"
  11.  
  12.  
  13. SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
  14. : QItemDelegate(parent)
  15. {
  16. }
  17.  
  18. QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
  19. const QStyleOptionViewItem &/* option */,
  20. const QModelIndex &/* index */) const
  21. {
  22. QSpinBox *editor = new QSpinBox(parent);
  23. editor->setMinimum(0);
  24. editor->setMaximum(100);
  25. editor->installEventFilter(const_cast<SpinBoxDelegate*>(this));
  26.  
  27. return editor;
  28. }
  29.  
  30. void SpinBoxDelegate::setEditorData(QWidget *editor,
  31. const QModelIndex &index) const
  32. {
  33. int value = index.model()->data(index, Qt::DisplayRole).toInt();
  34.  
  35. QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
  36. spinBox->setValue(value);
  37. }
  38.  
  39. void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
  40. const QModelIndex &index) const
  41. {
  42. QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
  43. spinBox->interpretText();
  44. int value = spinBox->value();
  45.  
  46. model->setData(index, value);
  47. }
  48.  
  49. void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
  50. const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
  51. {
  52. editor->setGeometry(option.rect);
  53. }
  54.  
  55. /***********************************************************************************************/
  56.  
  57.  
  58. ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
  59. : QItemDelegate(parent)
  60. {
  61. }
  62.  
  63. QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
  64. const QStyleOptionViewItem &/* option */,
  65. const QModelIndex &/* index */) const
  66. {
  67. QComboBox *editor = new QComboBox(parent);
  68. QStringList list ;
  69. list << "ankur" << "mitesh";
  70. editor->addItems(list);
  71. editor->installEventFilter(const_cast<ComboBoxDelegate*>(this));
  72.  
  73. return editor;
  74. }
  75.  
  76. void ComboBoxDelegate::setEditorData(QWidget *editor,
  77. const QModelIndex &index) const
  78. {
  79. QString value = index.model()->data(index, Qt::DisplayRole).toString();
  80.  
  81. QComboBox *comboBox = static_cast<QComboBox*>(editor);
  82. comboBox->addItem(value);
  83. }
  84.  
  85. void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
  86. const QModelIndex &index) const
  87. {
  88. QComboBox *comboBox = static_cast<QComboBox*>(editor);
  89. QString value = comboBox->currentText();
  90.  
  91. model->setData(index, value);
  92. }
  93.  
  94. void ComboBoxDelegate::updateEditorGeometry(QWidget *editor,
  95. const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
  96. {
  97. editor->setGeometry(option.rect);
  98. }
  99.  
  100. /***********************************************************************************************/
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. #ifndef DELEGATE_H
  2. #define DELEGATE_H
  3.  
  4. #include <QItemDelegate>
  5. #include <QModelIndex>
  6. #include <QObject>
  7. #include <QSize>
  8. #include <QSpinBox>
  9.  
  10. class SpinBoxDelegate : public QItemDelegate
  11. {
  12. Q_OBJECT
  13.  
  14. public:
  15. SpinBoxDelegate(QObject *parent = 0);
  16.  
  17. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
  18. const QModelIndex &index) const;
  19.  
  20. void setEditorData(QWidget *editor, const QModelIndex &index) const;
  21. void setModelData(QWidget *editor, QAbstractItemModel *model,
  22. const QModelIndex &index) const;
  23.  
  24. void updateEditorGeometry(QWidget *editor,
  25. const QStyleOptionViewItem &option, const QModelIndex &index) const;
  26. };
  27.  
  28. class ComboBoxDelegate : public QItemDelegate
  29. {
  30. Q_OBJECT
  31.  
  32. public:
  33. ComboBoxDelegate(QObject *parent = 0);
  34.  
  35. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
  36. const QModelIndex &index) const;
  37.  
  38. void setEditorData(QWidget *editor, const QModelIndex &index) const;
  39. void setModelData(QWidget *editor, QAbstractItemModel *model,
  40. const QModelIndex &index) const;
  41.  
  42. void updateEditorGeometry(QWidget *editor,
  43. const QStyleOptionViewItem &option, const QModelIndex &index) const;
  44. };
  45.  
  46. #endif
To copy to clipboard, switch view to plain text mode 

is there somehwere i m wrong .....