Results 1 to 2 of 2

Thread: How to centering the checkbox in qtableview

  1. #1
    Join Date
    Apr 2010
    Posts
    3
    Qt products
    Qt4

    Question How to centering the checkbox in qtableview

    hello, I want to draw a checkbox centered in the cell so I tried to subclass QStyledItemDelegate to draw the checkbox but it showed but won't centered and I tried to calculate QStyleOptionButton 's rect manually and it seem not the right way do to, so what is the right way to align item ? and one more problem I can't find a way to custom draw the header of qtableview , please help.

    Qt Code:
    1. void TestDelegate::paint(QPainter *painter,
    2. const QStyleOptionViewItem &option,
    3. const QModelIndex &index) const
    4. {
    5. if (index.column() == 1)
    6. {
    7. bool data = index.model()->data(index, Qt::DisplayRole).toBool();
    8. QStyleOptionButton checkboxstyle;
    9. checkboxstyle.rect = option.rect;
    10. int width = checkboxstyle.rect.width();
    11. checkboxstyle.rect.setLeft(checkboxstyle.rect.x()+width/2);
    12. if(data)
    13. checkboxstyle.state = QStyle::State_On|QStyle::State_Enabled;
    14. else
    15. checkboxstyle.state = QStyle::State_Off|QStyle::State_Enabled;
    16. QApplication::style()->drawControl(QStyle::CE_CheckBox, &checkboxstyle, painter);
    17. }
    18. else
    19. {
    20. QStyledItemDelegate::paint(painter, option, index);
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2010
    Posts
    3
    Qt products
    Qt4

    Default Re: How to centering the checkbox in qtableview

    I just got it done, hope this will work in general case
    Qt Code:
    1. void TestDelegate::paint(QPainter *painter,
    2. const QStyleOptionViewItem &option,
    3. const QModelIndex &index) const
    4. {
    5. if (index.column() == 1)
    6. {
    7. bool data = index.model()->data(index, Qt::DisplayRole).toBool();
    8. QStyleOptionButton checkboxstyle;
    9. QRect checkbox_rect = QApplication::style()->subElementRect(QStyle::SE_CheckBoxIndicator, &checkboxstyle);
    10. checkboxstyle.rect = option.rect;
    11. checkboxstyle.rect.setLeft(option.rect.x() +
    12. option.rect.width()/2 - checkbox_rect.width()/2);
    13. if(data)
    14. checkboxstyle.state = QStyle::State_On|QStyle::State_Enabled;
    15. else
    16. checkboxstyle.state = QStyle::State_Off|QStyle::State_Enabled;
    17.  
    18. QApplication::style()->drawControl(QStyle::CE_CheckBox, &checkboxstyle, painter);
    19. }
    20. else
    21. {
    22. QStyledItemDelegate::paint(painter, option, index);
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. boolean field checkbox QTableView
    By skuda in forum Qt Programming
    Replies: 4
    Last Post: 8th November 2010, 13:48
  2. CheckBox inside a QTableView
    By voilier92 in forum Qt Programming
    Replies: 2
    Last Post: 5th March 2010, 05:04
  3. Replies: 1
    Last Post: 9th August 2009, 15:27
  4. QTableView with a checkbox
    By maxpower in forum Qt Programming
    Replies: 17
    Last Post: 18th February 2007, 09:45
  5. CheckBox and selection in QTableView
    By Mike Krus in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2006, 20:31

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.