Results 1 to 6 of 6

Thread: Model/View, Delegates for my Data

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2016
    Posts
    7
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Model/View, Delegates for my Data

    I added QVariant to the code but the table still displays boolean as ture or false.

    Qt Code:
    1. QVariant RegisterModel::data(const QModelIndex &index, int role) const
    2. {
    3. ...
    4. case 4:
    5. return QVariant(Registers[row].wrProtect); // bool
    6. break;
    7. ...
    8.  
    9. case Qt::CheckStateRole:
    10.  
    11. if(col == 4)
    12. {
    13.  
    14. if(Registers[row].wrProtect == true)
    15. {
    16. return QVariant(Qt::Checked);
    17. }
    18. else
    19. {
    20. return QVariant(Qt::Unchecked);
    21. }
    22. }
    23. ...
    24. }
    To copy to clipboard, switch view to plain text mode 

    I want to use delegate. When I double-click the cell a new checkbox appers on the left in the cell. Checkbox is not displayed when I don't click.

    checkboxdelegate.h

    Qt Code:
    1. #ifndef CHECKBOXDELEGATE_H
    2. #define CHECKBOXDELEGATE_H
    3.  
    4. #include <QStyledItemDelegate>
    5. #include <QTableView>
    6. #include <QObject>
    7. #include <QDebug>
    8. #include <QCheckBox>
    9.  
    10. #define DEB qDebug().noquote() <<
    11.  
    12. class CheckBoxDelegate : public QStyledItemDelegate
    13. {
    14. Q_OBJECT
    15. public:
    16. explicit CheckBoxDelegate(QObject *parent = 0);
    17. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    18. void setEditorData(QWidget *editor, const QModelIndex &index) const;
    19. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
    20. QRect CheckBoxRect(const QStyleOptionViewItem &view_item_style_options) const;
    21. void updateEditorGeometry(QWidget *editor,
    22. const QStyleOptionViewItem &option,
    23. const QModelIndex &index) const;
    24. void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    25.  
    26. mutable QCheckBox *checkBox;
    27.  
    28. private slots:
    29. void setData(bool value);
    30. };
    31.  
    32. #endif // CHECKBOXDELEGATE_H
    To copy to clipboard, switch view to plain text mode 

    checkboxdelegate.cpp
    Qt Code:
    1. #include "checkboxdelegate.h"
    2. #include <QComboBox>
    3. #include <QCheckBox>
    4. #include <QRect>
    5. #include <QPainter>
    6. #include <QSpinBox>
    7. #include <QApplication>
    8. #include <QSignalMapper>
    9. #include "booleanwidget.h"
    10.  
    11. CheckBoxDelegate::CheckBoxDelegate(QObject *parent) :
    12. QStyledItemDelegate(parent)
    13. {
    14. }
    15.  
    16.  
    17. QWidget* CheckBoxDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
    18. {
    19.  
    20. checkBox = new QCheckBox(parent);
    21. QObject::connect(checkBox, SIGNAL(toggled(bool)), this, SLOT(setData(bool)));
    22. return checkBox;
    23. }
    24.  
    25. void CheckBoxDelegate::setEditorData ( QWidget *editor, const QModelIndex &index ) const
    26. {
    27.  
    28. if(QCheckBox *cb = static_cast<QCheckBox *>(editor)) {
    29.  
    30. bool checked = index.model()->data(index, Qt::DisplayRole /* Qt::EditRole */).toBool();
    31. DEB "Checked: " << checked;
    32. cb->setChecked(checked);
    33. }
    34. }
    35.  
    36. void CheckBoxDelegate::setModelData ( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
    37. {
    38.  
    39. if(QCheckBox *cb = static_cast<QCheckBox *>(editor))
    40. // save the current text of the combo box as the current value of the item
    41. model->setData(index, cb->isChecked());
    42. else
    43. QStyledItemDelegate::setModelData(editor, model, index);
    44. }
    45.  
    46.  
    47. void CheckBoxDelegate::updateEditorGeometry(QWidget *editor,
    48. const QStyleOptionViewItem &option,
    49. const QModelIndex &index) const {
    50.  
    51. editor->setGeometry(option.rect);
    52. }
    53.  
    54. void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    55. }
    56.  
    57. void CheckBoxDelegate::setData(bool value)
    58. {
    59. emit commitData(checkBox);
    60. }
    To copy to clipboard, switch view to plain text mode 

    What am I missing?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,328
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Model/View, Delegates for my Data

    I added QVariant to the code but the table still displays boolean as ture or false.
    No, I meant return literally "QVariant()" (an empty QVariant instance). Returning QVariant( Qt::Checked ) still results in the table seeing a Boolean value instead of nothing.

    Checkbox is not displayed when I don't click.
    I think you are misunderstanding what a delegate does. Delegates are -only- used when editing an item in a table or tree view. When the editing is finished, the editor is destroyed and the model is updated. So if you create a checkbox delegate, it will be displayed only when you are actually editing the cell with the Boolean item, otherwise it will not be visible.

    If you want something to be visible all of the time, you will have to implement the paint() method to draw the checkbox -image-.

    Personally, I think you are chasing this too far. You don't need a delegate for checkboxes, since QTableView already provides one. Did you try my suggestion of connecting a slot to the QItemSelectionModel?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 8
    Last Post: 16th July 2015, 19:44
  2. Replies: 1
    Last Post: 24th February 2011, 05:54
  3. Model / View and data structures
    By laszlo.gosztola in forum Newbie
    Replies: 0
    Last Post: 2nd December 2010, 05:13
  4. How to identify data from the model in the view?
    By csmithmaui in forum Qt Programming
    Replies: 8
    Last Post: 17th April 2010, 08:52
  5. data, model and tree view
    By larry104 in forum Qt Programming
    Replies: 17
    Last Post: 3rd July 2006, 14:43

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.