Results 1 to 5 of 5

Thread: Creating Custom Widgets With QStyleItemDelegate

  1. #1
    Join Date
    Jul 2010
    Location
    United States
    Posts
    13
    Thanks
    4
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Creating Custom Widgets With QStyleItemDelegate

    I am having a hard time getting custom delegates to work. I have a QTreeView where each item could require a separate input widget. One could need a simple QLineEdit, another could require a QComboBox, while others may require a custom widget which contains a QLabel next to a QPushButton. I can get the simple widgets to work with QStyledItemDelegate or QItemDelegate, but when I try to show my custom widget (QLabel next to a QPushButton) in createEditor, nothing shows up. Is it possible to show custom widgets as delegates? I have seen a lot of posts reference the paint() method in the delegate class, but I am not sure if I am suppose to draw the widget with the paint method?? Any ideas?

    Thanks

    Sorry, in the first line I say custom delegates, I meant to say custom widgets working in delegates

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Creating Custom Widgets With QStyleItemDelegate

    Is it possible to show custom widgets as delegates?
    Yes.

    I have seen a lot of posts reference the paint() method in the delegate class, but I am not sure if I am suppose to draw the widget with the paint method?
    For your case of composite widget, re-implementig paint() is not required.

    I can get the simple widgets to work with QStyledItemDelegate or QItemDelegate, but when I try to show my custom widget (QLabel next to a QPushButton) in createEditor, nothing shows up.
    The problem may be with the cell size in QTreeView, which may not be enough to fit the custom widget that was created using createEditor(). Try to create the editor widget with no parent, so that when editing is trigerred, you should see the custom composite widget pop up.

    I have put togeather a small example, not very clean but works.

    Qt Code:
    1. #include <QtGui>
    2. #include <QApplication>
    3.  
    4. class Editor : public QWidget
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. explicit Editor(const QModelIndex & index, QWidget * parent)
    10. : QWidget(parent)
    11. , mLineEdit(new QLineEdit)
    12. , mText(index.data().toString())
    13. {
    14. QHBoxLayout * hBoxLayout = new QHBoxLayout(this);
    15. QPushButton * pushButton = new QPushButton("Apply");
    16. hBoxLayout->addWidget(mLineEdit,2);
    17. hBoxLayout->addWidget(pushButton,1);
    18. connect(pushButton, SIGNAL(clicked()), SLOT(updateText()));
    19. mLineEdit->setText(mText);
    20. }
    21.  
    22. public:
    23. QString text(void) const
    24. {
    25. return mText;
    26. }
    27.  
    28. signals:
    29. void textChanged(const QString & text);
    30.  
    31. public slots:
    32. void setText(const QString & text)
    33. {
    34. mText = text;
    35. mLineEdit->setText(text);
    36. }
    37.  
    38. private slots:
    39. void updateText(void)
    40. {
    41. mText = mLineEdit->text();
    42. emit textChanged(mText);
    43. }
    44.  
    45. private:
    46. QLineEdit * mLineEdit;
    47. QString mText;
    48. };
    49.  
    50. class StyledItemDelegate : public QStyledItemDelegate
    51. {
    52. public:
    53. explicit StyledItemDelegate(QObject * parent = 0)
    54. : QStyledItemDelegate(parent)
    55. {
    56. ;
    57. }
    58.  
    59. virtual QWidget * createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
    60. {
    61. return new Editor(index, parent);
    62. }
    63.  
    64. virtual QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
    65. {
    66. return Editor(index, 0).sizeHint();
    67. }
    68.  
    69. protected:
    70. virtual void setEditorData(QWidget * editor, const QModelIndex & index) const
    71. {
    72. static_cast<Editor*>(editor)->setText(index.data().toString());
    73. }
    74.  
    75. virtual void updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    76. {
    77. static_cast<Editor*>(editor)->setGeometry(option.rect);
    78. }
    79.  
    80. virtual void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
    81. {
    82. model->setData(index, static_cast<Editor*>(editor)->text());
    83. }
    84. };
    85.  
    86. void addData(QStandardItemModel * model)
    87. {
    88. model->setRowCount(10);
    89. model->setColumnCount(2);
    90.  
    91. for(int r = 0; r < model->rowCount(); r ++)
    92. for(int c = 0; c < model->columnCount(); c++)
    93. model->setData(model->index(r, c), QString("Item-%1,%2").arg(r).arg(c), Qt::DisplayRole);
    94. }
    95.  
    96. int main(int argc, char ** argv)
    97. {
    98. QApplication app(argc, argv);
    99.  
    100. QTreeView treeView;
    101. StyledItemDelegate delegate;
    102.  
    103. addData(&model);
    104.  
    105. treeView.setModel(&model);
    106. treeView.setItemDelegate(&delegate);
    107. treeView.setEditTriggers(treeView.DoubleClicked);
    108. treeView.show();
    109.  
    110. return app.exec();
    111. }
    112.  
    113. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. The following user says thank you to Santosh Reddy for this useful post:

    smhall316 (18th April 2013)

  4. #3
    Join Date
    Jul 2010
    Location
    United States
    Posts
    13
    Thanks
    4
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Creating Custom Widgets With QStyleItemDelegate

    That was exactly the problem. I tried without parenting the custom widget and sure enough it showed up. Then I implemented the sizeHint and parented the custom widget and I was able to see the custom widget show up in the appropriate cell. Of course after I did the size hint I had very tall rows, but at least it is getting me in the right direction.

    On a sizing note, I assume I can resize my widget to fit in the default size of the cell. Would that be the way to fit the custom widget, or is there a more elegant way?

    Many thanks for your help!!

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Creating Custom Widgets With QStyleItemDelegate

    On a sizing note, I assume I can resize my widget to fit in the default size of the cell. Would that be the way to fit the custom widget, or is there a more elegant way?
    It depends, if you can make the widget fit cell, else you may need to use sizeHint() of delegate and change the cell size based on whether it is being edited, or just being displayed.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. #5
    Join Date
    Jul 2010
    Location
    United States
    Posts
    13
    Thanks
    4
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Creating Custom Widgets With QStyleItemDelegate

    Well I can use the size hint as you suggest, but it causes the row heights to increase too much. I can pass the row height to my widget I am using in the delegate. I even size the widget to match the row height that is currently in the treeview, however the widget seems to be offset in that index and does not show the whole thing.

    I have explored setting sizeHints, but I don't see how a user programmitically sets a size hint. Are you not allowed to dictate the size of your widgets?


    Added after 9 minutes:


    Nevermind my last question. It appears my problem has to do with my margins used in the layout. Simply adjusting setContentsMargins(0,0,0,0) fixed the problem!!
    Last edited by smhall316; 18th April 2013 at 21:47.

Similar Threads

  1. Replies: 1
    Last Post: 18th July 2012, 10:59
  2. Replies: 0
    Last Post: 30th April 2012, 16:17
  3. Replies: 0
    Last Post: 7th June 2010, 08:59
  4. Replies: 0
    Last Post: 15th May 2009, 16:38
  5. Creating Widgets
    By hylke in forum Qt Programming
    Replies: 2
    Last Post: 5th February 2006, 09:37

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.