Results 1 to 4 of 4

Thread: Painting QCheckbox into QTableView

  1. #1
    Join Date
    Aug 2008
    Location
    Texas, USA
    Posts
    44
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Painting QCheckbox into QTableView

    Hello, I believe I have read through most of the posts on QCheckbox's and QTableviews. It seems most people prefer to use a custom delegate which makes sense. I have used the spinboxdelegate as an example and I was successful in getting the QCheckbox to draw when entering the editrole of the table view, however when not in the edit role (i.e. paint()) function I have not yet figured out how to successfully draw/render the QCheckbox. Here are my two questions with code to follow:

    1. How can i make it so that a single click will toggle the checkbox as one would expect, instead of a double click to start editing and then a third click to toggle the checkbox?

    2. What am I doing wrong in my paint function?

    Thanks in advance!

    Qt Code:
    1. void cCheckboxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. bool Checked = index.model()->data(index, Qt::EditRole).toBool();
    4. QCheckBox CheckBox;
    5. CheckBox.setChecked(Checked);
    6. CheckBox.render(painter);
    7. }
    8.  
    9. QSize cCheckboxDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    10. {
    11. QCheckBox CheckBox;
    12. return CheckBox.sizeHint();
    13. }
    14.  
    15. QWidget *cCheckboxDelegate::createEditor(QWidget *parent,
    16. const QStyleOptionViewItem &/* option */,
    17. const QModelIndex &/* index */) const
    18. {
    19. QCheckBox *editor = new QCheckBox(parent);
    20. return editor;
    21. }
    22.  
    23. void cCheckboxDelegate::setEditorData(QWidget *editor,
    24. const QModelIndex &index) const
    25. {
    26. bool checked = index.model()->data(index, Qt::EditRole).toBool();
    27.  
    28. QCheckBox *checkBox = static_cast<QCheckBox*>(editor);
    29. checkBox->setChecked(checked);
    30. }
    31.  
    32. void cCheckboxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    33. const QModelIndex &index) const
    34. {
    35. QCheckBox *checkBox = static_cast<QCheckBox*>(editor);
    36. bool checked = checkBox->isChecked();
    37.  
    38. model->setData(index, checked, Qt::EditRole);
    39. }
    40.  
    41. void cCheckboxDelegate::updateEditorGeometry(QWidget *editor,
    42. const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
    43. {
    44. editor->setGeometry(option.rect);
    45. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Aug 2008
    Location
    Texas, USA
    Posts
    44
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Painting QCheckbox into QTableView

    Ok I got it to display the checkbox using the following code. However like I mentioned previous how would i make it so you don't have to triple click the checkbox to change the state?

    Thanks

    Qt Code:
    1. void cCheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. BtnStyle.state = QStyle::State_Enabled;
    4. if(index.model()->data(index, Qt::DisplayRole).toBool() == true)
    5. BtnStyle.state |= QStyle::State_On;
    6. else
    7. BtnStyle.state |= QStyle::State_Off;
    8. BtnStyle.direction = QApplication::layoutDirection();
    9. BtnStyle.rect = option.rect;
    10. QApplication::style()->drawControl(QStyle::CE_CheckBox,&BtnStyle,painter);
    11. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Painting QCheckbox into QTableView

    Are you aware of QStandardItem::setCheckable() ?

  4. #4
    Join Date
    Aug 2008
    Location
    Texas, USA
    Posts
    44
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Painting QCheckbox into QTableView

    Yes however I'm not using the StandItem, I'm subclassing QAbstractTableModel.

    Anyways I think I found the best solution as far as user interface goes. If I edit my Model and set the flag to Qt::ItemIsUserCheckable and then respond to the Qt::CheckStateRole in the setData and data() functions the checkbox works as expected. I.E. you don't have to first double click into the cell before the editor widget pops up, you simply just click the checkbox and your done.

    Here is some good sample code I used

    http://www.qtcentre.org/threads/2965...sUserCheckable

Similar Threads

  1. About QCheckBox
    By liushuo0826 in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 16th April 2010, 06:14
  2. QCheckBox value
    By Koas in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2009, 13:33
  3. Read-only QCheckBox
    By alu23 in forum Qt Programming
    Replies: 2
    Last Post: 22nd April 2008, 16:25
  4. QCheckbox
    By sonuani in forum Qt Programming
    Replies: 1
    Last Post: 19th February 2008, 13:12
  5. Insert QCheckBox into QTableView
    By wind in forum Qt Programming
    Replies: 3
    Last Post: 8th October 2006, 16:15

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.