PDA

View Full Version : QTableView headerData() color



starcontrol
16th April 2008, 09:41
Good morning all,
does anyone know, how to color the QTableViews header by using the standard delegate in Qts MV architecture? I tried this by using the roles as it does in the data() method, but in the headerData() nothing happens :-(



QVariant SchulungsplanModel::headerData(int section, Qt::Orientation orientation, int role) const
{
// HORIZONTAL HEADER: (timeline)
if (orientation == Qt::Horizontal)
{
if(role == Qt::BackgroundRole)
return QVariant(QBrush(QColor(Qt::green), Qt::SolidPattern));

if(role == Qt::DisplayRole)
{
if (section < m_horizontalHeaderList.size())
return m_horizontalHeaderList.at(section);
}
}

// VERTICAL HEADER: PUs
if (orientation == Qt::Vertical)
{
if(role == Qt::DisplayRole)
{
if (section < m_verticalHeaderList.size())
return m_verticalHeaderList.at(section);
}
}

return QAbstractTableModel::headerData(section, orientation, role);
}

wysota
16th April 2008, 11:18
The header doesn't use the delegate. It's a very limited class, so if you want something fancy, you'll have to subclass QHeaderView and implement it yourself.

starcontrol
16th April 2008, 14:21
Hi,
it is unbelievable, I compiled my project on Unix (HP-UX) and it worked !!!

It seems to be, that I detected a Qt bug ?!??
On Windows the roles statements doesn't work !

Do you have any idea ?

wysota
16th April 2008, 14:28
Qt tries to follow the platform style. If Windows doesn't allow header colours to be modified, they won't be. You could run your application with a different style (using -style stylename switch, i.e. -style plastique) on Windows and it'll probably work then.

starcontrol
16th April 2008, 14:44
this is the cheat ;-)

with a different style it works ! oh, oh, ....
and all the other roles are also supported ;-) foreground etc...

wysota
16th April 2008, 15:04
You can "cheat" even more by changing the style of the header only and leaving the rest of the application running the default style. That's how stylesheets work, by the way...