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