Results 1 to 4 of 4

Thread: QStyledItemDelegate with custom QWidget editor

  1. #1
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QStyledItemDelegate with custom QWidget editor

    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 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QStyledItemDelegate with custom QWidget editor

    Please provide a minimal compilable example reproducing the problem.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2010
    Location
    India
    Posts
    50
    Thanks
    1
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QStyledItemDelegate with custom QWidget editor

    CQuantityEditor(QWidget *parent = 0):QWidget(parent) ----> look at your constructor ~!

  4. The following user says thank you to agathiyaa for this useful post:

    stefanadelbert (14th July 2010)

  5. #4
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QStyledItemDelegate with custom QWidget editor

    Yes, indeed. That's what I was missing. How could I have missed that?!?

    @agathiyaa Thanks very much!

Similar Threads

  1. Custom item editor is now showing up.
    By TorAn in forum Qt Programming
    Replies: 1
    Last Post: 15th July 2010, 03:04
  2. How to use custom widget editor with QItemDelegate
    By wysman in forum Qt Programming
    Replies: 2
    Last Post: 20th May 2009, 18:20
  3. update custom QStyledItemDelegate after drag
    By dano in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2008, 18:29
  4. QTreeWidget Custom Editor Question
    By chaoticbob in forum Qt Programming
    Replies: 2
    Last Post: 30th October 2008, 07:06
  5. Replies: 5
    Last Post: 30th March 2008, 16:53

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.