在自定义QStyledItemDelegate上绘制背景像

在自定义QStyledItemDelegate上绘制背景像

问题描述:

我有一个表,并且正试图在example from the qt documentation之后添加一个自定义的代表。但是,尽管背景色似乎是正确的,但在选中该行的情况下似乎缺少叠加层(请注意,在评级单元格与该行的其余部分之间,列与蓝色背景之间的差异)。 最简单的方法是像标准代表那样画背景吗?在自定义QStyledItemDelegate上绘制背景像

对于QItemDelegate似乎有drawBackground,但是QStyledItemDelegate没有这种功能。不幸的是,它也会在其左侧的单元格中看到稍微更亮的矩形(上下比背景小1px)。

enter image description here

你说你根据Qt的StarDelegate例如仿照您的委托。就像一个实验一样。你可以尝试根据这个代码,您的代理模型:

void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, 
const QModelIndex &index) const 
{ 
    QStyledItemDelegate::paint(painter, option, index); 
    if (index.data().canConvert<StarRating>()) { 
     StarRating starRating = qvariant_cast<StarRating>(index.data()); 
     QStyledItemDelegate::paint(painter, option, index); 
     starRating.paint(painter, option.rect, option.palette, 
      StarRating::ReadOnly); 
    } 
} 

这也是从Qt的例子中,StarDelegate,但我改变了它一下,让无论表格单元格的第一个默认的背景画。那么,如果它是StarRating单元格,星星。我希望你能以这种方式获得默认的风格背景。没有保证,我没有自己尝试。 :-)

+0

这也会使用默认委托渲染渲染单元格内容(即,星级评分的int将被渲染为文本。 – ted