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
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTableView with a checkbox

    I'd stick with the delegate thing. Otherwise you'll have to reimplement the model without really needing it. The delegate will give you much more flexibility.

  2. #2
    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

    Okay, so I see in the docs how to subclass ItemDelegate and make my own. However, it appears you assign the delegate for the whole view. I only want to change the delegate for one column. How is this accomplished?

    mAx

  3. #3
    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

    Qt 4.2 introduces new functions:


    In Qt 4.1 you'll just have to check the column of the passed model index:
    Qt Code:
    1. void MyItemDelegate::delegateFunction(const QModelIndex& index, ...)
    2. {
    3. if (index.isValid() && index.column() == INTERESTING_COLUMN)
    4. {
    5. // appropriate column in question, do the custom thing
    6. }
    7. else
    8. {
    9. // pass to the base class implementation
    10. QItemDelegate::delegateFunction(index, ...);
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  4. #4
    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

    Okay, thanks for the help I am getting close now. I converted the included exmaple for the sponbox delegate to a checkbox delegate and compiled it and it worked great. However, once I changed the code fome setItemDelegate to setItemDelegateForColumn, it no longer worked at all. Do I need to change something in the example code for the custom delegate to work with just one column? I am using 4.2.

    Thanks
    mAx

  5. #5
    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

    Anybody have an idea? I just tried 4.2.1 but no love there either.

    mAx

  6. #6
    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

    Looks like QAbstractItemView::itemDelegateForRow/Column are not used pretty much for anything (yet?)... Anyway, I already gave you a solution which works with old good "normal" delegates.. and it has been discussed earlier..
    J-P Nurmi

  7. #7
    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

    Okay, I edited the spinbox example again and with your code it properly displays a checkbox in the first column. However, I tried to use the exact same code in my program and it doesn't work. The column and row headers are present but there is no data, nor checkboxes or even grid lines. What did i do wring?

    Thanks
    mAx

  8. #8
    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

    Does everything work correctly without the custom delegate? Which QItemDelegate functions have you actaully overridden? Did you by any chance override QItemDelegate::paint()?
    J-P Nurmi

  9. #9
    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 

  10. #10
    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

  11. #11
    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.