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.
void TestDelegate
::paint(QPainter *painter,
{
if (index.column() == 1)
{
bool data = index.model()->data(index, Qt::DisplayRole).toBool();
checkboxstyle.rect = option.rect;
int width = checkboxstyle.rect.width();
checkboxstyle.rect.setLeft(checkboxstyle.rect.x()+width/2);
if(data)
checkboxstyle.
state = QStyle::State_On|QStyle
::State_Enabled;
else
checkboxstyle.
state = QStyle::State_Off|QStyle
::State_Enabled;
}
else
{
QStyledItemDelegate::paint(painter, option, index);
}
}
void TestDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (index.column() == 1)
{
bool data = index.model()->data(index, Qt::DisplayRole).toBool();
QStyleOptionButton checkboxstyle;
checkboxstyle.rect = option.rect;
int width = checkboxstyle.rect.width();
checkboxstyle.rect.setLeft(checkboxstyle.rect.x()+width/2);
if(data)
checkboxstyle.state = QStyle::State_On|QStyle::State_Enabled;
else
checkboxstyle.state = QStyle::State_Off|QStyle::State_Enabled;
QApplication::style()->drawControl(QStyle::CE_CheckBox, &checkboxstyle, painter);
}
else
{
QStyledItemDelegate::paint(painter, option, index);
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks