void RatingDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,const QModelIndex &index) const { QStyleOptionViewItemV4 opt(option); QStyle *style = opt.widget ? opt.widget->style() : QApplication::style(); style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget); const int left = option.rect.left(); const int top = option.rect.top(); const int width = option.rect.width(); const int height = option.rect.height(); //Create base pixmap QPixmap pixmap(width, height); pixmap.fill(Qt::transparent); QPainter p(&pixmap); p.translate(-option.rect.topLeft()); //Paint rating int rating = index.data(Qt::DisplayRole).toInt(); StarRating starRating = StarRating(rating, StarRating::Medium); starRating.setRating(rating); QSize ratingSize = starRating.sizeHint(); int ratingLeft = left + 2; int ratingTop = top + (height - ratingSize.height())/2; QRect ratingRect = QRect(QPoint(ratingLeft, ratingTop), ratingSize); starRating.setPoint(ratingRect.topLeft()); starRating.paint(&p); p.end(); //Draw finished pixmap painter->drawPixmap(option.rect.topLeft(), pixmap); }
//! [0] void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if (index.data().canConvert<StarRating>()) { StarRating starRating = qvariant_cast<StarRating>(index.data()); if (option.state & QStyle::State_Selected) painter->fillRect(option.rect, option.palette.highlight()); starRating.paint(painter, option.rect, option.palette, StarRating::ReadOnly); } else { QStyledItemDelegate::paint(painter, option, index); } //! [0] }