I'm using a custom table model derived from QAbstractTableModel
.
I've overwritter headerData()
and I can change the font color for individual row-headers (or column-header, but I'm all about rows here) by returning the color on Qt::ForegroundRole
if(role == Qt::ForegroundRole)
return Qt::green;
But if I go for Qt::BackgroundRole
to set the background color of the header cells, nothing happens.
if(role == Qt::BackgroundRole)
return Qt::red;
I set a breakpoint on the return
and it is reached. But nothing happens :-(
Any ideas on where I'm wrong?
1) You can also achieve it by using own item delegates - inherit from QStyledItemDelegate or whatever else, reimplement one method and set it to view.
2) For specific table or header view, use style that respects brushes:
AFAIK the role colours are equivalent to setting a palette colour, the
QStyle
drawing the header cells is free to ignore it.I've had trouble using
QPalette
or style sheets to set arbitrary colours on widgets. Text tends to work, as do 'window' coloured backgrounds (aQPushButton
background for example), but text entry field backgrounds don't (QLineEdit
for example). But AFAIK it's down to the particularQStyle
implementation, so will vary not only across widgets, but also across platforms. The only certain way to get things exactly how you want is to reimplementQStyle
(a big job), or paint it manually inpaintEvent(..)
(difficult to follow the currentQStyle
and still lots of code).