I do not know what is the benefit from item role?
Somehow you need to recover the combobox index (an integer) that is associated with the string that is the DisplayRole or EditRole data for the QModelIndex you are about to edit.

One way to do it would be to search the set of strings that are loaded into the combobox (QCombobox::findText()). Another way is to store it in your model as a special integer field that you set when the delegate calls setData() on your model. setData() is going to give you the currently selected string, which you can then convert to the integer. The use of Qt::UserRole is a way for you to store and retrieve this integer easily.

Qt Code:
  1. // Method 1: look up the string in the combobox
  2. void MyComboDelegate::setEditorData( QWidget * pEditor, const QModelIndex & index ) const
  3. {
  4. QItemDelegate::setEditorData( pEditor, index ); // use base class to fill the strings
  5.  
  6. QComboBox * pCombo = qobject_cast< QComboBox * >( pEditor );
  7. if ( pCombo )
  8. {
  9. QString displayStr = index.data( Qt::DisplayRole ).toString();
  10. int selected = pCombo->findText( displayStr );
  11. if ( selected > -1 )
  12. pCombo->setCurrentIndex( selected );
  13. }
  14. }
  15.  
  16. // Method 2: use Qt::UserRole to store the index in the model
  17. void MyComboDelegate::setEditorData( QWidget * pEditor, const QModelIndex & index ) const
  18. {
  19. QItemDelegate::setEditorData( pEditor, index ); // use base class to fill the strings
  20.  
  21. QComboBox * pCombo = qobject_cast< QComboBox * >( pEditor );
  22. if ( pCombo )
  23. {
  24. int selected = index.data( Qt::UserRole ).toInt();
  25. if ( selected > -1 )
  26. pCombo->setCurrentIndex( selected );
  27. }
  28. }
  29.  
  30. void MyComboDelegate::setModelData( QWidget * pEditor, QAbstractItemModel * pModel, const QModelIndex & index ) const
  31. {
  32. QItemDelegate::setModelData( pEditor, pModel, index ); // base class sets Qt::DisplayRole string
  33.  
  34. QComboBox * pCombo = qobject_cast< QComboBox * >( pEditor );
  35. if ( pCombo )
  36. {
  37. int selected = pCombo->currentIndex();
  38. pModel->setData( index, QVariant( selected ), Qt::UserRole );
  39. }
  40. }
  41.  
  42.  
  43. void MyModel::setData( const QModelIndex & index, const QVariant & value, int role )
  44. {
  45. if ( index.column() == 0 && role == Qt::UserRole )
  46. {
  47. int selected = value.toInt();
  48. // store "selected" as a field in your model for that row.
  49. }
  50. else if ( role == Qt::EditRole )
  51. {
  52. // do whatever you would do otherwise to update your model
  53. }
  54. }
  55.  
  56. QVariant MyModel::data( const QModelIndex & index, int role ) const
  57. {
  58. if ( index.column() == 0 && role == Qt::UserRole )
  59. {
  60. int selected; // retrieve "selected" from the field in your model for that row.
  61. return QVariant( selected );
  62. }
  63. else if ( role == Qt::DisplayRole ) (or EditRole or whatever)
  64. {
  65. // do whatever you would do otherwise to get data from your model
  66. }
  67. }
To copy to clipboard, switch view to plain text mode