Lex
Lex

Reputation: 61

Qt - QTableview row color with delegates

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:

Picture when the colors are working

Picture after resizing, after clipping, it sometimes stops to this, can be fixed by defocusing the main window (click outside of it), accessing the context menu and other things like that

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

Answers (1)

Lex
Lex

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

Related Questions