I've created a custom QStyledItemDelegate which uses a custom QWidget for an editor.

When the editor widget is created it's not being painted in the cell that's being edited. It's painted as a separate dialog window.

The editor should contain a QSpinBox and a QToolButton.

Here are my delegate and editor classes:

Delegate

Qt Code:
  1. class CQuantityItemDelegate : public QStyledItemDelegate
  2. {
  3. Q_OBJECT
  4.  
  5. public:
  6. CQuantityItemDelegate(QWidget *parent = 0) : QStyledItemDelegate(parent) {}
  7.  
  8. void paint(QPainter *painter, const QStyleOptionViewItem &option,
  9. const QModelIndex &index) const;
  10. QSize sizeHint(const QStyleOptionViewItem &option,
  11. const QModelIndex &index) const;
  12. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
  13. const QModelIndex &index) const;
  14. void setEditorData(QWidget *editor, const QModelIndex &index) const;
  15. void setModelData(QWidget *editor, QAbstractItemModel *model,
  16. const QModelIndex &index) const;
  17.  
  18. private slots:
  19. void commitAndCloseEditor();
  20. };
  21.  
  22. void
  23. CQuantityItemDelegate::paint(
  24. QPainter* painter,
  25. const QStyleOptionViewItem& option,
  26. const QModelIndex& index) const
  27. {
  28. QStyledItemDelegate::paint(painter, option, index);
  29. }
  30.  
  31. CQuantityItemDelegate::sizeHint(
  32. const QStyleOptionViewItem& option,
  33. const QModelIndex& index) const
  34. {
  35. return QStyledItemDelegate::sizeHint(option, index);
  36. }
  37.  
  38. CQuantityItemDelegate::createEditor(
  39. QWidget* parent,
  40. const QStyleOptionViewItem& option,
  41. const QModelIndex& index) const
  42. {
  43. CQuantityEditor* editor = new CQuantityEditor(parent);
  44. connect(editor, SIGNAL(editingFinished()),
  45. this, SLOT(commitAndCloseEditor()));
  46. return editor;
  47. }
  48.  
  49. void
  50. CQuantityItemDelegate::setEditorData(
  51. QWidget* editor,
  52. const QModelIndex& index) const
  53. {
  54. int value = index.model()->data(index, Qt::EditRole).toInt();
  55. CQuantityEditor* quantityEditor = static_cast<CQuantityEditor*>(editor);
  56. quantityEditor->SetQuantity(value);
  57. }
  58.  
  59. void
  60. CQuantityItemDelegate::setModelData(
  61. QWidget* editor,
  62. const QModelIndex& index) const
  63. {
  64. QStyledItemDelegate::setModelData(editor, model, index);
  65. }
  66.  
  67. void
  68. CQuantityItemDelegate::commitAndCloseEditor()
  69. {
  70. CQuantityEditor* editor = qobject_cast<CQuantityEditor*>(sender());
  71. emit commitData(editor);
  72. emit closeEditor(editor);
  73. }
To copy to clipboard, switch view to plain text mode 

Editor

Qt Code:
  1. class CQuantityEditor : public QWidget
  2. {
  3. Q_OBJECT
  4.  
  5. public:
  6. CQuantityEditor(QWidget *parent = 0) {
  7. quantitySpinBox = new QSpinBox(this);
  8. // confirm = new QToolButton(parent);
  9. // confirm->setIcon(QIcon(":/Icons/Box/Tick/green"));
  10. // confirm->setToolButtonStyle(Qt::ToolButtonIconOnly);
  11. //
  12. // QHBoxLayout* hLayout = new QHBoxLayout(parent);
  13. // hLayout->addWidget(quantitySpinBox, 1);
  14. // hLayout->addWidget(confirm, 0);
  15. // hLayout->setSpacing(0);
  16. // hLayout->setMargin(0);
  17. // setLayout(hLayout);
  18. //
  19. // connect(confirm, SIGNAL(released()),
  20. // this, SIGNAL(editingFinished())
  21. // );
  22. }
  23.  
  24. void SetQuantity(const int& quantity) {
  25. quantitySpinBox->setValue(quantity);
  26. }
  27. int Quantity() { return quantitySpinBox->value(); }
  28.  
  29. signals:
  30. void editingFinished();
  31.  
  32. protected:
  33. void paintEvent(QPaintEvent* e) {
  34. QWidget::paintEvent(e);
  35. }
  36.  
  37. private:
  38. QSpinBox* quantitySpinBox;
  39. QToolButton* confirm;
  40. };
To copy to clipboard, switch view to plain text mode