Results 1 to 4 of 4

Thread: Help needed with QItemDelegate

  1. #1
    Join Date
    Jan 2008
    Posts
    58
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Help needed with QItemDelegate

    Hi,

    I'm busy with my QItemDelegate and my custom model!
    But there are a few things that I don't get quite well.

    Correct me if I'm wrong!
    The painter function of the QItemDelegate is invoked when the view sees the delegated cell.
    The createEditor function is invoked when the user wants to edit the cell.
    The setEditorData function is invoked after the widget is created.
    And the setModelData is invoked when the user finished editing.

    So with this knowledge in the back of my head I started coding!

    Qt Code:
    1. #include "checkBoxDelegate.h"
    2.  
    3. #include <QtGui>
    4.  
    5. CheckBoxDelegate::CheckBoxDelegate(QObject *parent)
    6. :QItemDelegate(parent)
    7. {
    8. qDebug("CheckBoxDelegate::CheckBoxDelegate(): constructing delegate");
    9. }
    10.  
    11. void CheckBoxDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    12. {
    13. qDebug("CheckBoxDelegate::paint(): painting the delegate");
    14.  
    15. const QAbstractItemModel *model = index.model();
    16.  
    17. if(!model)
    18. QItemDelegate::paint(painter, option, index);
    19.  
    20. if(index.data() == 2)
    21. {
    22. painter->fillRect(option.rect, QBrush(QColor("green")));
    23. }else{
    24. painter->fillRect(option.rect, QBrush(QColor("red")));
    25.  
    26. }
    27.  
    28. }
    29.  
    30.  
    31. QWidget *CheckBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option , const QModelIndex &index) const
    32. {
    33. qDebug("CheckBoxDelegate::createEditor(): create checkbox");
    34. const QAbstractItemModel *model = index.model();
    35.  
    36. if(!model)
    37. QItemDelegate::createEditor(parent, option, index);
    38.  
    39. QCheckBox *checkbox = new QCheckBox(parent);
    40. if(QVariant(model->data(index, Qt::EditRole)).toInt() == 2)
    41. checkbox->setCheckState(Qt::Checked);
    42. else
    43. checkbox->setCheckState(Qt::Unchecked);
    44.  
    45. return checkbox;
    46.  
    47. }
    48. void CheckBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    49. {
    50. qDebug("CheckBoxDelegate::setEditorData(): set editor data");
    51.  
    52. QCheckBox *checkbox = qobject_cast<QCheckBox *>(editor);
    53.  
    54.  
    55. if(!checkbox)
    56. return QItemDelegate::setEditorData(editor, index);
    57.  
    58. const QAbstractItemModel *model = index.model();
    59.  
    60.  
    61. if(QVariant(model->data(index, Qt::EditRole)).toInt() == 2)
    62. checkbox->setCheckState(Qt::Checked);
    63. else
    64. checkbox->setCheckState(Qt::Unchecked);
    65.  
    66.  
    67. }
    68.  
    69. void CheckBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    70. {
    71. qDebug("CheckBoxDelegate::setModelData(): Set model data");
    72.  
    73. if(!index.isValid())
    74. return;
    75.  
    76. return QItemDelegate::setModelData(editor, model, index);
    77. }
    To copy to clipboard, switch view to plain text mode 

    The idea is to show a checkbox with a green or red background. Indicating whether it's checked or not. (It's a little bit strange but it has to be a field that catches your attention).

    And the background drawing works, its green when the state is 2 and red when it's 0.
    But I can't see the widget until I start editing the cell.

    Is there a way that the checkbox is all ready visible before I start editing.

    I have the same problem with the date delegate, it isn't visible when the table is loaded!

    Greetings

    Cyberboy
    Last edited by cyberboy; 8th July 2008 at 20:31. Reason: spelling error

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

    Default Re: Help needed with QItemDelegate

    The editor is created only when a particular cell needs editing, so in normal conditions at most one editor per view exists. You can make a persistant editor, but doing that for more than a few items slows down the application terribly.

    Now the good news - you don't need a custom delegate The standard delegate can draw the checkbox for you if you use the Qt::CheckStateRole and return Qt::ItemIsUserCheckable among the flags for a particular item.

  3. #3
    Join Date
    Jan 2008
    Posts
    58
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Help needed with QItemDelegate

    Oke, so I have to pass a Qt::CheckStateRole in my model for the particular column/cell and the standard delegate will take care of the rest?

    (There isn't a standard for a QDateEdit, is it? At least I didn't found one in the docs!)

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

    Default Re: Help needed with QItemDelegate

    Date time edit has to be handled using a real editor, but you can emulate its looks using a custom delegate.

Similar Threads

  1. QItemDelegate
    By cyberboy in forum Qt Programming
    Replies: 10
    Last Post: 27th June 2008, 17:41
  2. QItemDelegate, QTreeView and scrollbars
    By SiLiZiUMM in forum Qt Programming
    Replies: 11
    Last Post: 6th May 2008, 17:23
  3. Completer on QItemDelegate
    By gemidjy in forum Qt Programming
    Replies: 6
    Last Post: 31st March 2008, 10:29
  4. Return a pointer from a QItemdelegate
    By SailinShoes in forum Qt Programming
    Replies: 5
    Last Post: 12th March 2008, 20:07
  5. premature call to setmodeldata with QItemDelegate
    By placebo in forum Qt Programming
    Replies: 1
    Last Post: 25th November 2007, 17:39

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
  •  
Qt is a trademark of The Qt Company.