Results 1 to 18 of 18

Thread: QTableView with a checkbox

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2006
    Posts
    60
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: QTableView with a checkbox

    Yes, everything works fine if I do not use setItemDelegate. I overrode createEditor, setEditor, setModelData, and updateModelGeometry. As I said before, my code works fine by itself, just not inside my db program.

    Applying the delegate:
    Qt Code:
    1. if(viewInit)
    2. {
    3. model->submitAll();
    4. delete model;
    5. }
    6. int rowCount = 0;
    7. model = new QSqlTableModel();
    8. model->setTable(tableName);
    9. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    10. model->select();
    11. model->removeColumn(8);
    12. rowCount = model->rowCount();
    13. ui.tableTV->setModel(model);
    14. checkboxDelegate delegate;
    15. ui.tableTV->setItemDelegate(&delegate);
    16. ui.tableTV->resizeColumnToContents(2);
    17. ui.tableTV->hideColumn(0);
    18. ui.tableTV->setEditTriggers(QAbstractItemView::CurrentChanged);
    19. ui.tableTV->setAlternatingRowColors(true);
    20. ui.tableTV->scrollToBottom();
    To copy to clipboard, switch view to plain text mode 

    The delegate:
    Qt Code:
    1. checkboxDelegate::checkboxDelegate(QObject *parent)
    2. : QItemDelegate(parent)
    3. {
    4. }
    5.  
    6. QWidget *checkboxDelegate::createEditor(QWidget *parent,
    7. const QStyleOptionViewItem &option,
    8. const QModelIndex &index) const
    9. {
    10. if(index.isValid() && index.column() == checkboxCol)
    11. {
    12. QCheckBox *editor = new QCheckBox(parent);
    13. editor->installEventFilter(const_cast<checkboxDelegate*>(this));
    14. return editor;
    15. }
    16. else
    17. {
    18. return QItemDelegate::createEditor(parent, option, index);
    19. }
    20. }
    21.  
    22. void checkboxDelegate::setEditorData(QWidget *editor,
    23. const QModelIndex &index) const
    24. {
    25. if(index.isValid() && index.column() == checkboxCol)
    26. {
    27. QString value = index.model()->data(index, Qt::DisplayRole).toString();
    28.  
    29. QCheckBox *checkBox = static_cast<QCheckBox*>(editor);
    30. if(value == "Y")
    31. checkBox->setCheckState(Qt::Checked);
    32. else
    33. checkBox->setCheckState(Qt::Unchecked);
    34. }
    35. else
    36. {
    37. QItemDelegate::setEditorData(editor, index);
    38. }
    39. }
    40.  
    41. void checkboxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    42. const QModelIndex &index) const
    43. {
    44. if(index.isValid() && index.column() == checkboxCol)
    45. {
    46. QCheckBox *checkBox = static_cast<QCheckBox*>(editor);
    47. QString value;
    48. if(checkBox->checkState() == Qt::Checked)
    49. value = "Y";
    50. else
    51. value = "N";
    52.  
    53. model->setData(index, value);
    54. }
    55. else
    56. {
    57. QItemDelegate::setModelData(editor, model, index);
    58. }
    59. }
    60.  
    61. void checkboxDelegate::updateEditorGeometry(QWidget *editor,
    62. const QStyleOptionViewItem &option, const QModelIndex &index) const
    63. {
    64. if(index.isValid() && index.column() == checkboxCol)
    65. editor->setGeometry(option.rect);
    66. else
    67. QItemDelegate::updateEditorGeometry(editor, option, index);
    68.  
    69. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableView with a checkbox

    Allocate the delegate object on the heap so it won't get destroyed as soon as it gets out of scope:
    Qt Code:
    1. checkboxDelegate* delegate = new checkboxDelegate(this);
    2. ui.tableTV->setItemDelegate(delegate);
    To copy to clipboard, switch view to plain text mode 

    Edit: oops, forgot the important keyword new
    Last edited by jpn; 1st November 2006 at 19:18.
    J-P Nurmi

  3. #3
    Join Date
    Oct 2006
    Posts
    60
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: QTableView with a checkbox

    That did it, thanks alot!!!!

    mAx

Similar Threads

  1. QTableView sorting
    By gabriels in forum Qt Programming
    Replies: 11
    Last Post: 6th October 2010, 17:13
  2. checkbox
    By nErnie in forum Qt Programming
    Replies: 1
    Last Post: 25th September 2006, 21:59
  3. CheckBox and selection in QTableView
    By Mike Krus in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2006, 20:31
  4. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42
  5. Multi-line messages in QTableView
    By Conel in forum Qt Programming
    Replies: 6
    Last Post: 13th April 2006, 13:49

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.