QHeaderView background color for each section
-
I have the same problem as in this topic:
https://forum.qt.io/topic/144127/how-paint-a-background-section-in-a-qheaderviewI have a QTableView with dynamic number of headers and would like to have them in different colors (logic will come later, just need to paint right now)
My last attempt was:
void HeaderView::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>()); }
Also I dont know how to override initStyleOptionForIndex() as @Christian-Ehrlicher mentioned in the topic above. Any help?
-
I have the same problem as in this topic:
https://forum.qt.io/topic/144127/how-paint-a-background-section-in-a-qheaderviewI have a QTableView with dynamic number of headers and would like to have them in different colors (logic will come later, just need to paint right now)
My last attempt was:
void HeaderView::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>()); }
Also I dont know how to override initStyleOptionForIndex() as @Christian-Ehrlicher mentioned in the topic above. Any help?
@rudag said in QHeaderView background color for each section:
bg.value<QBrush>()
Do you really return a QBrush in headerData() for Qt::BackgroundRole?
-
I have the same problem as in this topic:
https://forum.qt.io/topic/144127/how-paint-a-background-section-in-a-qheaderviewI have a QTableView with dynamic number of headers and would like to have them in different colors (logic will come later, just need to paint right now)
My last attempt was:
void HeaderView::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>()); }
Also I dont know how to override initStyleOptionForIndex() as @Christian-Ehrlicher mentioned in the topic above. Any help?
This post is deleted! -
@rudag said in QHeaderView background color for each section:
bg.value<QBrush>()
Do you really return a QBrush in headerData() for Qt::BackgroundRole?
@Christian-Ehrlicher Yes, like below:
modelM->clear(); modelM->setColumnCount(ops.count()); modelM->setRowCount(funcs.count()); modelM->setHorizontalHeaderLabels(ops); modelM->setVerticalHeaderLabels(funcs); //setting only one headerData to test modelM->setHeaderData(0, Qt::Horizontal, QBrush(Qt::blue), Qt::BackgroundRole);
-
@Christian-Ehrlicher Yes, like below:
modelM->clear(); modelM->setColumnCount(ops.count()); modelM->setRowCount(funcs.count()); modelM->setHorizontalHeaderLabels(ops); modelM->setVerticalHeaderLabels(funcs); //setting only one headerData to test modelM->setHeaderData(0, Qt::Horizontal, QBrush(Qt::blue), Qt::BackgroundRole);
@rudag said in QHeaderView background color for each section:
modelM->setHeaderData(0, Qt::Horizontal, QBrush(Qt::blue), Qt::BackgroundRole);
Here you only call setData()... but i asked what data() returns.
-
I have the same problem as in this topic:
https://forum.qt.io/topic/144127/how-paint-a-background-section-in-a-qheaderviewI have a QTableView with dynamic number of headers and would like to have them in different colors (logic will come later, just need to paint right now)
My last attempt was:
void HeaderView::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>()); }
Also I dont know how to override initStyleOptionForIndex() as @Christian-Ehrlicher mentioned in the topic above. Any help?
@rudag said in QHeaderView background color for each section:
I have a QTableView with dynamic number of headers and would like to have them in different colors (logic will come later, just need to paint right now)
My last attempt was:
You didn't say what is unsatisfactory about this attempt.
Is there another reason to implement a custom header view? QHeaderView already supports a background color.
#include <QApplication> #include <QTableView> #include <QStandardItemModel> #include <QColor> int main(int argc, char *argv[]) { QApplication a(argc, argv); QStandardItemModel model; QStandardItem h1("test"); model.appendRow(&h1); model.setHeaderData(0, Qt::Orientation::Horizontal, QColorConstants::Green, Qt::ItemDataRole::BackgroundRole); QTableView view; view.setModel(&model); view.show(); return a.exec(); }
-
@rudag said in QHeaderView background color for each section:
modelM->setHeaderData(0, Qt::Horizontal, QBrush(Qt::blue), Qt::BackgroundRole);
Here you only call setData()... but i asked what data() returns.
@Christian-Ehrlicher said in QHeaderView background color for each section:
Here you only call setData()... but i asked what data() returns.
It's working now. I didn't know I had to call QHeaderView's show() explicitly.
painter->drawText(rect, Qt::AlignCenter, model()->headerData(logicalIndex, Qt::Horizontal, Qt::DisplayRole).toString());
But it's lost the mouse hover effect, bold text when column is selected, etc.. do I really have to write all these effects?
@jeremy_k said in QHeaderView background color for each section:
You didn't say what is unsatisfactory about this attempt.
Is there another reason to implement a custom header view? QHeaderView already supports a background color.
Tried your setHeaderData() and didn't work. I really would like a solution without subclass. I tried like bellow and background is okay, but it doesn't show the text.
QStandardItem *i = new QStandardItem("Test"); i->setBackground(QBrush(Qt::yellow)); modelM->setHorizontalHeaderItem(0, i);
-
@Christian-Ehrlicher said in QHeaderView background color for each section:
Here you only call setData()... but i asked what data() returns.
It's working now. I didn't know I had to call QHeaderView's show() explicitly.
painter->drawText(rect, Qt::AlignCenter, model()->headerData(logicalIndex, Qt::Horizontal, Qt::DisplayRole).toString());
But it's lost the mouse hover effect, bold text when column is selected, etc.. do I really have to write all these effects?
@jeremy_k said in QHeaderView background color for each section:
You didn't say what is unsatisfactory about this attempt.
Is there another reason to implement a custom header view? QHeaderView already supports a background color.
Tried your setHeaderData() and didn't work. I really would like a solution without subclass. I tried like bellow and background is okay, but it doesn't show the text.
QStandardItem *i = new QStandardItem("Test"); i->setBackground(QBrush(Qt::yellow)); modelM->setHorizontalHeaderItem(0, i);
@rudag said in QHeaderView background color for each section:
Tried your setHeaderData() and didn't work. I really would like a solution without subclass. I tried like bellow and background is okay, but it doesn't show the text.
Did you try the full and unmodified program? That suggests that you've found a bug in Qt. If it was instead adapted into a non-working program, that doesn't tell me anything meaningful. The snippets of the nonworking program posted so far aren't enough to work with.