C++ (Qt)class CBtn_Delegate : public QItemDelegate{ Q_OBJECT; QString m_sIconPath; public: CBtn_Delegate( QObject * parent = 0 ); QWidget * createEditor( QWidget * parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const; void setEditorData( QWidget * editor, const QModelIndex &index ) const; void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const; void updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; void paint( QPainter * painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const; inline void setIconPath( QString sVal ) { m_sIconPath = sVal; }};
C++ (Qt)void CBtn_Delegate::paint( QPainter * painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const{ QWidget * w = dynamic_cast<QWidget *>( painter->device() ); if ( w ) { QStylePainter p( w ); QStyleOptionButton opt; opt.icon = QIcon( m_sIconPath ); opt.iconSize = QSize( 24, 24 ); opt.initFrom( w ); opt.rect = option.rect; bool bChecked = index.data().toInt() > 0; if ( bChecked ) opt.state |= QStyle::State_On; p.drawControl( QStyle::CE_PushButton, opt ); } drawFocus( painter, option, option.rect );}
C++ (Qt)void CBtn_Delegate::paint( QPainter * painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const{ QWidget * w = dynamic_cast<QWidget *>( painter->device() ); if ( w ) { QStylePainter p( w ); QStyleOptionButton opt; opt.icon = QIcon( m_sIconPath );