How to change the background of QTableView header?
-
First, I've done following in the constructor of QHeaderView sublass:
TableHeader::TableHeader(QTableView * parent) : QHeaderView(Qt::Horizontal, parent){ ... auto p = palette(); p.setColor(QPalette::Base, Qt::darkGray); setPalette(p); ... }
this palette thing works for
QPlainTextEdit
,QTreeView
background and all cells ofQTableView
. It didn't work for the header:Second, added this in QHeaderView sublass:
void TableHeader::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, Qt::darkGray); }
didn't work either. Finally, tried this:
void TableWidget::queryDatabase(){ tableModel->setTable(tableName); tableHeader->resetBoxes(tableModel->columnCount()); tableModel->select(); while (tableModel->canFetchMore()) tableModel->fetchMore(); for (int i = 0; i < tableModel->columnCount(); i++) tableModel->setHeaderData(i, Qt::Horizontal, QBrush(Qt::darkGray), Qt::BackgroundRole); }
also doesn't work.
-
Qt is not always consistent with itself :(
For one TableView i'm doing a mix of stylesheet and palette settings:
BankTableView::BankTableView(QWidget* parent) : QTableView(parent) { QString style = R"( BankTableView { background-color: transparent; } BankTableView QHeaderView { background-color: transparent;} )"; setStyleSheet(style); QPalette p = palette(); p.setColor(QPalette::Highlight, QColor(255,100,30,150)); p.setColor(QPalette::Disabled, QPalette::Highlight, QColor(200,200,200,65)); p.setColor(QPalette::HighlightedText, QColor(50,50,50)); setPalette(p);
-
@mpergand said in How to change the background of QTableView header?:
Qt is not always consistent with itself :(
It is - as stated in the docs the stylesheet always wins: "Note: If Qt Style Sheets are used on the same widget as functions that set the appearance of widgets, such as QWidget::setFont() or QTreeWidgetItem::setBackground(), style sheets will take precedence if the settings conflict."
-
@Christian-Ehrlicher, is there a way to do that without stylesheet? using css in desktop app, in and of itself, is inconsistent, it's not electron.
-
@Emon-Haque said in How to change the background of QTableView header?:
is there a way to do that without stylesheet?
No, not on e.g. windows as described in the link I gave you: "For this kind of customization, style sheets are much more powerful than QPalette. For example, it might be tempting to set the QPalette::Button role to red for a QPushButton to obtain a red push button. However, this wasn't guaranteed to work for all styles, because style authors are restricted by the different platforms' guidelines and (on Windows and macOS) by the native theme engine."
-
@Christian-Ehrlicher
Style sheets should not exist in the first place.
If you want to change the appearance of a object, create a subclass, that's basic OOP.I don't understand why Qt doesn't use native widgets, there are always some woes, on Mac it's ugly !
Just to illustrate a few:
- text not centered
- no margins for the line edit
- wrong selection color for the menu
and I'm not talking about Linux ...
-
Halfway through. If I replace the painSection with these:
void TableHeader::paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const{ // painter->fillRect(this->contentsRect(), Qt::transparent); // painter->fillRect(this->contentsRect(), Qt::transparent); painter->fillRect(rect, Qt::transparent); painter->drawText(rect, Qt::AlignHCenter, model()->headerData(logicalIndex, Qt::Horizontal, Qt::DisplayRole).toString()); }
it looks like this:
on the right side there's still some unexpected white area. Tried to fill the entire
rect()
inpaintEvent
like this:void TableHeader::paintEvent(QPaintEvent *event){ QPainter painter(this); painter.fillRect(rect(), Qt::transparent); }
it does fill the entire area including the header text and I get these in Qt Creator output:
QWidget::paintEngine: Should no longer be called QPainter::begin: Paint device returned engine == 0, type: 1
-
@Christian-Ehrlicher, first of all it's not to bash Qt, I use a few Qt apps everyday and the small app through which I've started to explore Qt Widgets, which I post as screenshots/animations, in most of my posts has a WPF counterpart and sometime I compare the performance and features. Here's a few contrast:
-
I can zoom in a Datagrid with a 100k rows in WPF BUT I can't in QTableView. @SGaist, suggested for paging BUT I'd say improve UI/Data virtualization.
-
in WPF, I can write app without a single line of XAML, that's my preferred way and that's easier, Last time I checked QML, I couldn't even subclass a
Rectangle
for customization. -
to some extent css can be powerful BUT obviously not for moderate/complex UI like this, never mind my screenshots:
that's the dashboard of an app I've written for myself and may be some other people also use that as I've shared the code/app in github. There's some fairly complex animation involved in that app and every control has been customized to make my life easier and there isn't any third party library other than WPF framework in that app.
I wish all the best for Qt and, hopefully, I'll keep exploring more of Qt.
-
-
@Emon-Haque
Yes, WPF has more powerful, out-of-the-box features for Windows Presentation.OTOH, it doesn't work across platforms, nor for that matter does it provide, say, networking or bluetooth code like Qt does.
They are very different.
So you can either work within what Qt does provide, or go for a dedicated Windows-platform with "fancier" features, as you please.
-
@JonB, it's networking feature for sure! I've written a basic stock exchange in WPF (both server and client) and at that time I could make 120+ transaction per second AND in client side I've real time price/volume chart.
-
@Emon-Haque said in How to change the background of QTableView header?:
it's networking feature for sure!
I'm not sure what you are saying here. WPF is for UI presentation. All the other stuff you are talking about is from .NET. Which is a totally different topic. If you're now talking about .NET facilities compared to Qt that is a whole different matter. I merely picked some random aspect of Qt I could think of which is not UI. I could have mentioned something else, like
QTextToSpeech
orQPdfWriter
orQXmlReader
or whatever. Qt offers a lot more than just UI stuff. -
@Emon-Haque
LOL. So you don't mean just WPF. You mean compared against .NET as well? Perhaps you mean that .NET includes Windows as well, so that's all part of WPF, and Qt fails to provide its own OS?I just don't get what point you are trying to make. Yes WPF has fancier stuff than Qt, I said that. And btw, what do you estimate the user base for .NET/WPF is compared to Qt, or the revenue of MS vs TQtC?
This is a user forum for Qt, so we work with what we get. We use Qt, not develop it. What would you like us to do about the various features you say (correctly) that WPF has which are not present in Qt?
-
@mpergand, for the horizonalheader, Here's a way. First in the constructor of
QHeaderView
subclass addsetStretchLastSection(true);
and in thepaintSection
have only those two lines posted above. It'll look like this: -
@JonB, I believe QML team respects WPF masterminds a lot. I read, a long time ago, that QML was inspired by WPF XAML tech. They've one additional and important thing, you don't need XAML to write cool WPF apps. Hope that, someday, Qt users will also have that cool feature.
We can get into war on the pros and cons of different techs BUT that's not the way! Copy good things of others. C# teams haven't done anything wrong by copying Java and none, other than trolls, blame or humiliate them for that. Now, Java is far behind C# in terms of features.
Respect masterminds and keep contributing.
-
With subclasses you can do customizations easily;
I'm using QPainterPath, QGradient a lot and it works well.IMHO style sheets look more like a hack than anything else.
-