QHeaderView vertical headers do not change background color
-
I am trying to change the background color of
QHeaderView
using the following code:QVariant FileSystemModel::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation == Qt::Vertical) { switch (role) { case static_cast<int>(Role::Marked): return m_markedFiles.contains(getPathFromRow(section)); break; case Qt::BackgroundRole: { // QColor color; // bool isMarked = m_markedFiles.contains(getPathFromRow(section)); // if (isMarked) // return color.fromString("#FF5000"); // return color; // TODO: Fix this } break; case Qt::FontRole: { QFont font; bool isMarked = m_markedFiles.contains(getPathFromRow(section)); if (isMarked) { font.setBold(true); font.setItalic(true); return font; } return font; } case Qt::DisplayRole: return section; break; default: return QVariant(); break; } } else if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { switch (m_column_list.at(section)) { case ColumnType::FileName: return "Name"; case ColumnType::FileSize: return "Size"; case ColumnType::FileModifiedDate: return "Last Modified"; case ColumnType::FilePermission: return "Permissions"; default: return QVariant(); } } return QVariant(); }
I did the same thing with the rows of
QTableView
and it worked. But for some reason, the header view doesn't respect it. The font changes, but the foreground and background roles do not. -
I would guess the style ignores it. E.g. the windows style does not allow modifying the header color but fusion does iirc.
-
So, there is no way to override the defaults ?
-
So, there is no way to override the defaults ?
@dheerajshenoy create a subclass of QHeaderView, QProxyStyle or use the
fusion
style. -
I created a subclass of
QProxyStyle
and implemented thedrawControl
function. It still does not respect the headerData backgroundRole and foregroundRole -
Not knowing how you implemented it does not help finding where the issue is.
-
To solve this, I ended up creating a subclass of the
QHeaderView
and added my background color logic (background using the background role of the underlying model associated) inside thepaintSection
method. -