Reputation: 61
I have quite a very specific problem with the row coloring for QTableView
, the main problem is coloring the whole background of the row, but also using delegates on different columns, here is what I tried:
data()
of the custom data model: the problem with this implementation is that the background of custom delegates (for columns) does not change when coloring happens.QStyledItemDelegate
for row, this method works perfectly for coloring, the problem is that I cannot assign any other column delegates for that particular row.QStyledItemDelegate
for column, and the painter fills the rectangle
of the whole row, this seems almost right for me, all columns are colored, the problem is, on resize, I got clipping and from time to time, the background color disappears on the other columns, screenshots below.Picture when the colors are working
The thing is that some columns use the default editor/delegate, and some use a custom one.
The question is, what would be the best method to implement this?
Or, can I paint the whole row and restrict the other delegates from repainting their background?
Upvotes: 1
Views: 1858
Reputation: 61
I managed to find a solution:
My main problem was that my delegates were not taking the background color from the model in order to paint it, I fixed this by copying the implementation of the background from the QItemDelegate
implementation, the snippet that I needed to implement in the paint()
method of my custom delegate is this:
// draw the background color
if (option.showDecorationSelected && (option.state & QStyle::State_Selected)) {
QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
? QPalette::Normal : QPalette::Disabled;
painter->fillRect(option.rect, option.palette.brush(cg, QPalette::Highlight));
} else {
QVariant value = index.data(Qt::BackgroundColorRole);
if (value.isValid() && qvariant_cast<QColor>(value).isValid())
painter->fillRect(option.rect, qvariant_cast<QColor>(value));
}
With this, I can now take the color from the model and paint the delegate background.
Upvotes: 2