formatting a QTableView header
-
wrote on 7 Nov 2018, 20:56 last edited by
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).
-
wrote on 7 Nov 2018, 23:32 last edited by
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).
Moderatorswrote on 8 Nov 2018, 07:33 last edited by kshegunov 11 Aug 2018, 07:34You 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.
-
wrote on 8 Nov 2018, 08:33 last edited by VRonin 11 Aug 2018, 15:13
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 -
You might want to try just adding
QApplication::setStyle(QStyleFactory::create("Fusion"));
to yourmain()
. The fusion style supports the styling ofQHeaderView
s even on windowswrote on 8 Nov 2018, 15:24 last edited by@VRonin using that style, along with the style sheet, did in fact paint my header background red. Thank you!
Now, for creating a separation line under the header...is it recommended that I try to use a style sheet, or QStyles?
-
wrote on 8 Nov 2018, 16:13 last edited by
Try the fast way, use stylesheet
-
wrote on 8 Nov 2018, 17:55 last edited by
Interesting results...I tried this:
ui->tableView->horizontalHeader()->setStyleSheet("border-bottom: 5px solid red");
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?
-
wrote on 8 Nov 2018, 18:05 last edited by VRonin 11 Aug 2018, 19:15
-
wrote on 8 Nov 2018, 18:16 last edited by
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?"
-
wrote on 8 Nov 2018, 19:17 last edited by
it's the sub element inside the headerview. That style applies to every single row/column header cell
-
wrote on 9 Nov 2018, 15:59 last edited by
Ah, I see (I think). Well, the upshot is that I can remove all of my stylesheet settings. The use of the Fusion style gave me the desired look:
Which is fortunate, because this attempt at stylesheet setting didn't work:
ui->tableView->horizontalHeader()->setStyleSheet("border-bottom: 5px solid red");
Not sure why (the red background worked).
40/41