Qt中,如果有需求,在代理类(继承自QStyledItemDelegate)中,重写paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index)函数,自己实现绘制一个按钮。
QStyleOptionButton* button = m_btnMap.value(index);
if (!button)
{
button = new QStyleOptionButton();
button->rect = option.rect.adjusted(4, 4, -4, -4);
button->text = tr("按钮");
button->state |= QStyle::State_Enabled;
(const_cast<CContentTableDelegate *>(this))->m_btnMap.insert(index, button);
}
painter->save();
if (option.state & QStyle::State_Selected)
{
painter->fillRect(option.rect, option.palette.highlight());
}
painter->restore();
QApplication::style()->drawControl(QStyle::CE_PushButton, button, painter);