Results 1 to 4 of 4

Thread: Help needed with QItemDelegate

Threaded View

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

    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 21:31. Reason: spelling error

Similar Threads

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