0

I have successfully used the following Qt code in my python application to color horizontal headers in a QTableWidget with distinct background colors:

header2 = widget.horizontalHeaderItem(2)
purple = QColor(139,103,153)
header2.setBackground(QtGui.QBrush(purple)) 

header3 = widget.horizontalHeaderItem(3)
orange = QColor(237,168,89)
header3.setBackground(QtGui.QBrush(orange)) 

This is what it looks like: enter image description here

However, in my C++ Qt plugin for an existing application, the equivalent logic does not work for the QTableWidget horizontal headers, but does work for table cells or QTableWidgetItem.

The C++ application has it's own style with a dark theme. If I replace the style with plastique, then my code works.

QApplication::setStyle("plastique"); 

However, this is not an acceptable solution, as it changes the appearance of the entire application.

I have searched the web and stackoverflow for a solution and this post seems relevant. It appears to be subclassing QHeaderView, but the implementation is a little advanced for me. Unfortunately, the post above does not solve work in my case.

Am I on the right track? Can someone provide a little more detail about the .h and .cpp and main() implementation?

panofish
  • 7,578
  • 13
  • 55
  • 96
  • What do you not understand about the solution? To say that it is advanced for you does not imply that it is not duplicated. – eyllanesc Nov 03 '17 at 15:25
  • Possible duplicate of [What does QHeaderView::paintSection do such that all I do to the painter before or after is ignored](https://stackoverflow.com/questions/30847252/what-does-qheaderviewpaintsection-do-such-that-all-i-do-to-the-painter-before) – eyllanesc Nov 03 '17 at 15:25
  • It is not clear how to implement. What do the .h and .cpp files look like. Does the code in main resemble my python logic and now just work? – panofish Nov 03 '17 at 15:54
  • The code should be something similar to the following, I recommend reading about inheritance since that is the primary part `class Header: public QHeaderView{ protected: void 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()); } }` – eyllanesc Nov 03 '17 at 15:58
  • In the next question there is an example: https://stackoverflow.com/questions/36366163/read-model-data-with-custom-qheaderview – eyllanesc Nov 03 '17 at 16:00
  • You should paste the image, not link to it. Many firewalls block links, so fewer people can help you. – Thomas Matthews Nov 03 '17 at 16:06
  • How do I make the QTableWidget header utilize the newly defined class called Header? – panofish Nov 03 '17 at 16:06
  • your_tableWidget->setHorizontalHeader(new Header); – eyllanesc Nov 03 '17 at 16:51
  • I called tableWidget->setHorizontalHeader(new Header); Code runs without errors, but my QTableWidget now appears without a header and it appears that paintSection is never called? – panofish Nov 03 '17 at 18:58

0 Answers0