Qt的代理类中使用QPainter绘制按钮

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);


版权声明:本文为ELaXiaoSi原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。