formatting a QTableView header
-
I'm not doing anything with the header except what I posted above. DeviceModel is my own class that has the actual Qt model as a member:
class DeviceModel : public QObject { Q_OBJECT private: QAbstractItemModel *m_model; // the main model
@mzimmers said in formatting a QTableView header:
I'm not doing anything with the header except what I posted above. DeviceModel is my own class that has the actual Qt model as a member.
Indeed. My bad, sorry. It's rather odd. As far as I can tell this should be enough to have your header have a red background. As a follow up question: Do you use style sheets?
-
@mzimmers said in formatting a QTableView header:
I'm not doing anything with the header except what I posted above. DeviceModel is my own class that has the actual Qt model as a member.
Indeed. My bad, sorry. It's rather odd. As far as I can tell this should be enough to have your header have a red background. As a follow up question: Do you use style sheets?
@kshegunov said in formatting a QTableView header:
As a follow up question: Do you use style sheets?
No, not in this app.
-
@kshegunov said in formatting a QTableView header:
As a follow up question: Do you use style sheets?
No, not in this app.
I just tested a project of mine, and it works correctly. Here's what I did:
#include <QBrush> QVariant MyModel::headerData(int section, Qt::Orientation orientation, int role) const { if (/*role != Qt::DisplayRole || */orientation != Qt::Horizontal || section > columnCount()) return QVariant(); if (role == Qt::DisplayRole) return columns.byIndex(section).displayName(); if (role == Qt::BackgroundRole) return QBrush(Qt::red); return QVariant(); }
I observe the background of the table view's header to be red. It's really odd why it doesn't work for you. Can you retrieve the header data just after you've set it to the model to see if it's correctly stored? I.e.
m_model->setHeaderData(TAG_SERIALNUMBER, Qt::Horizontal, QBrush(Qt::red), Qt::BackgroundRole); QBrush shouldBeRed = m_model->headerData(TAG_SERIALNUMBER, Qt::Horizontal, Qt::BackgroundRole);
PS: The test was done with Qt 5.11.2 on Linux.
-
QBrush brush = variant.value<QBrush>();
However you can inspect the data directly in the variant as well, as far as I can see in the watch.
-
@mzimmers: are you working on windows? https://bugreports.qt.io/browse/QTBUG-31804
-
@mzimmers: are you working on windows? https://bugreports.qt.io/browse/QTBUG-31804
@Christian-Ehrlicher yes, I am, and that would seem to be the problem here. Plus, there's this:
Widget::Widget(DeviceModel *d, QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ... ui->tableView->horizontalHeader()->setBackgroundRole(QPalette::Window);
So, do I need to create a QStyle for the underlining, and set that on my header view?
-
@Christian-Ehrlicher yes, I am, and that would seem to be the problem here. Plus, there's this:
Widget::Widget(DeviceModel *d, QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ... ui->tableView->horizontalHeader()->setBackgroundRole(QPalette::Window);
So, do I need to create a QStyle for the underlining, and set that on my header view?
@mzimmers said in formatting a QTableView header:
So, do I need to create a QStyle for the underlining, and set that on my header view?
I'd advise at least trying if that will fix the issue.
-
@mzimmers said in formatting a QTableView header:
Do I need to derive a subclass from QStyle to do this?
Yes. But maybe try QProxyStyle instead, so you have less work to do.
-
-
OK, I have a little spare time now. I've copied this code (from the page):
class MyProxyStyle : public QProxyStyle { public: int styleHint(StyleHint hint, const QStyleOption *option = 0, const QWidget *widget = 0, QStyleHintReturn *returnData = 0) const override { if (hint == QStyle::SH_UnderlineShortcut) return 0; return QProxyStyle::styleHint(hint, option, widget, returnData); } };
What would you suggest I use instead of SH_UnderlineShortcut to test whether my styling is taking effect? (Given the trouble we were having earlier.)
Thanks.
-
Hi,
How did you set your custom style ?
-
What if you set it application wide ?
-
For what it's worth, this line of code produces expected results:
ui->tableView->setStyleSheet("background-color: red");
But this one does not:
ui->tableView->horizontalHeader()->setStyleSheet("background-color: red");
So the problem seems to be in the header object, doesn't it?
-
I can try that. What I was asking for, though, was a style hint that would be readily apparent in my app. The one in the example might work perfectly, but I'd never see it without implementing any shortcuts (at least I think not).
You don't need a style hint if you're trying to override the painting. You'd have to reimplement
QStyle::drawControl
and handle theQStyle::CE_Header
control. Something like this:class MyProxyStyle : public QProxyStyle { public: void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = nullptr) const override { if (element != QStyle::CE_Header) { baseStyle()->drawControl(element, option, painter, widget); return; } // Paint here ... } };
It's been a long time since I last played with the styles, but I hope this is of help.