formatting a QTableView header
-
@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.
-
You might want to try just adding
QApplication::setStyle(QStyleFactory::create("Fusion"));
to yourmain()
. The fusion style supports the styling ofQHeaderView
s even on windows -
Interesting results...I tried this:
ui->tableView->horizontalHeader()->setStyleSheet("border-bottom: 5px solid red");
And got this:
I must be malforming the stylesheet (I pulled the contents directly from a CSS example I found online), but the curious (to me) part is I'm now getting an underline (actually a bottom border, I guess) across the entire header, but only the middle column is painted red (which I don't understand either).
Thoughts?
-
-
Disregard my image above: the setting of the red background was due to a separate experiment I'd been performing on the model. It was disguised when I set the entire header view to red, then reappeared when I removed that CSS in favor of the bottom border.
I'd actually read that page you referenced, but I don't fully understand the syntax. I can see that everything in the braces is Qt style information, but what is the context in which they're used? For example, in this:
QHeaderView::section
What is "section?"