Results 1 to 3 of 3

Thread: custom delegate for QSqlRelationalModel

  1. #1
    Join Date
    Jan 2011
    Posts
    32
    Thanks
    11
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default custom delegate for QSqlRelationalModel

    This is what I have implemented in my custom delegate:

    Qt Code:
    1. class pcheckboxdelegate : public QStyledItemDelegate
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. pcheckboxdelegate(QObject *parent = 0) : QStyledItemDelegate(parent) {}
    7.  
    8. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
    9. const QModelIndex &index) const
    10. {
    11. QCheckBox *editor = new QCheckBox(parent);
    12. return editor;
    13. }
    14.  
    15. void setEditorData(QWidget *editor, const QModelIndex &index) const
    16. {
    17. bool value = index.model()->data(index, Qt::DisplayRole).toBool();
    18. QCheckBox *checkBox = static_cast<QCheckBox*>(editor);
    19. if (value == true ) {
    20. checkBox->setCheckState(Qt::Checked);
    21. } else {
    22. checkBox->setCheckState(Qt::Unchecked);
    23. }
    24. }
    25.  
    26. void setModelData(QWidget *editor, QAbstractItemModel *model,
    27. const QModelIndex &index) const
    28. {
    29. QCheckBox *checkBox = static_cast<QCheckBox*>(editor);
    30. if(checkBox->checkState() == Qt::Checked) {
    31. model->setData(index, true);
    32. } else {
    33. model->setData(index, true);
    34. }
    35. }
    36.  
    37. void updateEditorGeometry(QWidget *editor,
    38. const QStyleOptionViewItem &option, const QModelIndex &index) const
    39. {
    40. editor->setGeometry(option.rect);
    41. }
    42. };
    To copy to clipboard, switch view to plain text mode 

    I am trying to achieve that I get checkbox in the cell for boolean type in database instead of true or false text that I am getting.
    With this code I can only see checkbox after doubleclick on the cell. Even then "true" text stays in the cell.
    Could you help me implement delegate that would always show checkbox and I will not have to doubleclick the cell to use it?

  2. #2
    Join Date
    Aug 2008
    Posts
    45
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: custom delegate for QSqlRelationalModel

    If check box is all you need, then you can use the default delegate. It can render checkboxes if you set Qt::CheckStateRole role to one of the values of Qt::CheckState.

  3. #3
    Join Date
    Jan 2011
    Posts
    32
    Thanks
    11
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: custom delegate for QSqlRelationalModel

    How to do that? Should I reimplement data and setData function in subclassed QSqlRelationalModel?
    Last edited by mak_user; 21st June 2011 at 22:17.

Similar Threads

  1. Error in custom delegate
    By philacorns in forum Newbie
    Replies: 4
    Last Post: 21st April 2010, 05:41
  2. Replies: 0
    Last Post: 11th March 2010, 07:41
  3. Custom Model? Custom View? Custom Delegate?
    By Doug Broadwell in forum Newbie
    Replies: 4
    Last Post: 11th February 2010, 20:23
  4. Replies: 0
    Last Post: 1st February 2010, 11:00
  5. Question about custom view (or custom delegate)
    By skuda in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2009, 20:06

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.