Hi community,
in my code I have added a view derived from QTableView having 2 columns and x rows ( x is not fixed but grows ).
The first column contains text and there I show the content of the model that stores internally a string of strings.
The second columns have show a QComboBox where the user should be able to choose one of the combobox options (also a QString). I am using a class derived from QStyledItemDelegate for that purpose.
I have no problems with the first column, I can correctly add new rows with the right text.
My problem is with the column #2, I can show the combobox with the options but, after choosing one, if I click anywhere in the view, the combobox value clears.
So seems I am doing something wrong in storing and read the corect combobox value.

Here the code I used to create the item delegate

*.h

Qt Code:
  1. #ifndef SELECTEDLISTITEMDELEGATE_H
  2. #define SELECTEDLISTITEMDELEGATE_H
  3.  
  4. #include <QStyledItemDelegate>
  5.  
  6. class SelectedListItemDelegate : public QStyledItemDelegate
  7. {
  8. Q_OBJECT
  9.  
  10. public:
  11. SelectedListItemDelegate(QObject *parent = nullptr);
  12. ~SelectedListItemDelegate() override;
  13.  
  14. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
  15. void setEditorData(QWidget *editor, const QModelIndex &index) const override;
  16. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
  17. };
  18.  
  19. #endif
To copy to clipboard, switch view to plain text mode 

and the cpp

Qt Code:
  1. #include "SelectedListItemDelegate.h"
  2. #include <QComboBox>
  3. #include <QDebug>
  4.  
  5. SelectedListItemDelegate::SelectedListItemDelegate(QObject* parent)
  6. : QStyledItemDelegate(parent)
  7. {
  8. }
  9.  
  10. SelectedListItemDelegate::~SelectedListItemDelegate()
  11. {
  12. }
  13.  
  14. QWidget *SelectedListItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
  15. {
  16. Q_UNUSED(option)
  17.  
  18. QComboBox *comboBox = new QComboBox(parent);
  19.  
  20. // I considere 3 options just as an example
  21. const int row = index.row();
  22. comboBox->addItem(QString("Option %1").arg(row));
  23. comboBox->addItem(QString("Option %1").arg(row));
  24. comboBox->addItem(QString("Option %1").arg(row));
  25.  
  26. return comboBox;
  27. }
  28.  
  29. // This routine does not set any value in the combobox
  30. void SelectedListItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  31. {
  32. QString value = index.model()->data(index, Qt::EditRole).toString();
  33. qDebug() << "Value:" << value; // Value is empty
  34. QComboBox *comboBox = qobject_cast<QComboBox*>(editor);
  35. comboBox->setCurrentIndex(comboBox->findText(value));
  36. }
  37.  
  38. // Seems that the combobox value is not correctly written because is not kept in the cell. If I click outside its cleared
  39. void SelectedListItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
  40. {
  41. QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
  42. QString value = comboBox->currentText();
  43. model->setData(index, value, Qt::EditRole);
  44. }
To copy to clipboard, switch view to plain text mode 

Can I have a help in understanding what's I am doing wrong?
Thanx