Hi there,

I can't get an editor for a cell in a table working. The table is displayed in a QTableView with a model implemented in a subclass of QAbstractTableModel. One column is to display a pen width parameter. This pen width is configured using a wizard. In the wizard a combo box is used which looks as follows:

Pen Width.PNG

The code for the model that is used by the combo box I found somewhere
in the internet (and adapted it for my purposes). It looks as follows:

Model class declaration:

Qt Code:
  1. class PenWidthModel : public QAbstractListModel {
  2.  
  3. public:
  4. PenWidthModel(QObject *parent = nullptr);
  5.  
  6. public:
  7. int rowCount(const QModelIndex &parent) const override;
  8. QVariant data(const QModelIndex &index, int role) const override;
  9.  
  10. private:
  11. static void initialize();
  12.  
  13. static std::unique_ptr<QMap<int, QIcon>> thePenWidths2Icons;
  14. };
To copy to clipboard, switch view to plain text mode 

Model class implementation:

Qt Code:
  1. unique_ptr<QMap<int, QIcon>>
  2. PenWidthModel::thePenWidths2Icons(nullptr);
  3.  
  4. PenWidthModel::PenWidthModel(QObject *parent) :
  5. if (thePenWidths2Icons.get() != nullptr) {
  6. return;
  7. }
  8. PenWidthModel::initialize();
  9. }
  10.  
  11. int PenWidthModel::rowCount(const QModelIndex &parent) const {
  12. if (parent.isValid())
  13. return 0;
  14.  
  15. return 5;
  16. }
  17.  
  18. QVariant PenWidthModel::data(const QModelIndex &index, int role) const {
  19. if (role == Qt::DecorationRole) {
  20. return (*thePenWidths2Icons)[index.row()];
  21. }
  22. return QVariant();
  23. }
  24.  
  25. void PenWidthModel::initialize() {
  26. thePenWidths2Icons = make_unique<QMap<int, QIcon>>();
  27. for (int i = 0; i < 6; ++i) {
  28. QPixmap pix(80, 14);
  29. pix.fill(Qt::white);
  30.  
  31. QBrush brush(Qt::black);
  32. QPen pen(brush, static_cast<double>(i + 1), Qt::SolidLine);
  33.  
  34. QPainter painter(&pix);
  35. painter.setPen(pen);
  36. painter.drawLine(2,7,78,7);
  37. painter.end();
  38. thePenWidths2Icons->insert(i, QIcon(pix));
  39. }
  40. }
To copy to clipboard, switch view to plain text mode 

As can be seen in the code for each of the available pen widths a QIcon object showing a pixmap with a line of appropriate thickness is provided (thickness of 0 is avoided).
This model works fine in conjunction with the wizard and the combo box used in it for selection of a pen width.

The pen width configured for a graph is later on shown in a table of graphs with their properties. In this table I want to be able to edit the pen width for a certain graph. In this context I want to reuse the PenWidthModel class. From my current understanding to edit data in a table in a non-standard way one has to use so called item delegates. So I implemented an item delegate that uses a QComboBox for editing of the pen width. My code looks as follows:

Item delegate class declaration:

Qt Code:
  1. class PenWidthDelegate : public QStyledItemDelegate {
  2. public:
  3. PenWidthDelegate(QObject *parent);
  4.  
  5. ~PenWidthDelegate();
  6.  
  7. public:
  8. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
  9. void setEditorData(QWidget *editor, const QModelIndex &index) const override;
  10. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
  11. };
To copy to clipboard, switch view to plain text mode 

Item delegate class implementation:

Qt Code:
  1. PenWidthDelegate::PenWidthDelegate(QObject *parent)
  2. : QStyledItemDelegate(parent) {
  3. }
  4.  
  5. PenWidthDelegate::~PenWidthDelegate() {
  6. }
  7.  
  8. QWidget *PenWidthDelegate::createEditor(QWidget *parent,
  9. const QStyleOptionViewItem &option,
  10. const QModelIndex &index) const {
  11. QComboBox *penWidthCB = new QComboBox(parent);
  12. penWidthCB->setModel(new PenWidthModel(parent));
  13. return penWidthCB;
  14. }
  15.  
  16. void PenWidthDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
  17. QComboBox *penWidthCB = static_cast<QComboBox*>(editor);
  18. penWidthCB->setCurrentIndex(index.model()->data(index, Qt::EditRole).toInt());
  19. }
  20.  
  21. void PenWidthDelegate::setModelData(QWidget *editor,
  22. const QModelIndex &index) const {
  23. QComboBox *penWidthCB = static_cast<QComboBox*>(editor);
  24. int ix = penWidthCB->currentIndex();
  25. model->setData(index, ix, Qt::EditRole);
  26. }
To copy to clipboard, switch view to plain text mode 

As can be seen in the code in the createEditor(...) method I create the QComboBox instance to be used and assign an instance of the PenWidthModel class as its model. In the setEditorData(...) method I set the index of the to be shown pen width icon. and in the setModelData(...) method I write the selected pen width back to the table's model. But unfortunately the combo box shown when I initiate editing of a pen width does not display the icons representing the different pen widths. Instead it looks as follows:

Pen Width Editor.PNG

Obviously the icons provided by the model class are not displayed. Additionally the result of an edit is not immediately shown but seems to be shown only after one has clicked somewhere besides the edited cell.

Obviously I'm doing something wrong ...
Any help would be highly appreciated.