This question is further development of this post and is different, though may seem similar as this one.
I am trying to reimplement QHeaderView::paintSection
It is obvious why the first fillRect doesn't work. Everything that you paint before paintSection is overridden by base painting.
The second call is more interesting.
Usually all paint methods preserves painter state. It means that when you call paint it looks like the painter state hasn't been changed.
Nevertheless QHeaderView::paintSection spoils the painter state.
To bypass the issue you need to save and restore the state by yourself:
void Header::paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const
{
QVariant bg = model()->headerData(logicalIndex, Qt::Horizontal, Qt::BackgroundRole);
painter->save();
QHeaderView::paintSection(painter, rect, logicalIndex);
painter->restore();
if(bg.isValid())
painter->fillRect(rect, bg.value<QBrush>());
}