Hi!

I have been studying the Spin Box Delegate Example and I think I understood how it works.

Now I am trying to create the delegate for a qcombobox.
I have transformed all the SpinBox functions but I cannot do the same for the setEditorData().Any ideas?

Qt Code:
  1. #include <QtGui>
  2. #include "delegate.h"
  3. #include <delegate.h>
  4. #include <comboboxdelegate.h>
  5.  
  6. ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
  7. : QItemDelegate(parent)
  8. {
  9. }
  10.  
  11. QWidget *ComboBoxDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &/* option */,
  12. const QModelIndex &/* index */) const
  13. {
  14. QComboBox *editor = new QComboBox(parent);
  15. editor->addItem("a");
  16. editor->addItem("b56");
  17. editor->addItem("cc");
  18. return editor;
  19. }
  20.  
  21. void ComboBoxDelegate::setEditorData(QWidget *editor,
  22. const QModelIndex &index) const
  23. {
  24. // QString str = index.model()->data(index, Qt::EditRole);
  25.  
  26. // QComboBox *comboBox = static_cast<QComboBox*>(editor);
  27. // comboBox->setValue(value);
  28. // comboBox->setCurrentIndex(value);
  29.  
  30.  
  31. }
  32.  
  33. void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
  34. const QModelIndex &index) const
  35. {
  36. QComboBox *comboBox = static_cast<QComboBox*>(editor);
  37. //comboBox->interpretText();//is this important for the QComboBox delegate??
  38. QString str = comboBox->currentText();
  39.  
  40. model->setData(index, str, Qt::EditRole);
  41. }
  42.  
  43. void ComboBoxDelegate::updateEditorGeometry(QWidget *editor,
  44. const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
  45. {
  46. editor->setGeometry(option.rect);
  47. }
To copy to clipboard, switch view to plain text mode