1
votes

I have a table and am trying to add a custom delegate following the example from the qt documentation. However, while the background color seems to be correct the overlay seems to be missing, in case the row is selected (Note the difference in the column with the blue background between the rating cell and the rest of the row). What is the easiest way to draw the background like the standard delegate does?

For QItemDelegate there seems to be drawBackground, however there is no such function for QStyledItemDelegate. And unfortunatly drawBackground, also ommits the slightly brighter rectangle (1 px smaller on top and bottom than the background) which you see in the cells to the left of it.

enter image description here

1

1 Answers

0
votes

You said you modeled your delegate according to the Qt StarDelegate example. Just as an experiment. Could you try to model your delegate according to this code:

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

It is also the StarDelegate from the Qt example, but I changed it a bit so that regardless of the table cell first the default background is painted. Then, if it is the StarRating cell, the stars. My hope is that you get this way the default styled background. No guarantee, I did not try it myself. :-)