0

Basically, I need to customize the background of a QTableView modeled by QStandardItemModel

I was able to change the background color of the QTableView by setting a stylesheet as below, but with this the, the header column background still stays white.

reportTableView = """
                    #reportTableView{
                        background-color:#333;
                        color:#fff;
                        border:1px solid #555;
                    }
                    #reportTableView::section{
                        background-color:#333;
                    }
                """
reportContentsWidgetModel = QStandardItemModel(4, 4)

reportColumnNames = ["website", "https://neehack.com", "neehack.com", "pubs"]
reportContentsWidgetModel.setHorizontalHeaderLabels(reportColumnNames)
reportContentsWidget = QTableView()
reportContentsWidget.resizeColumnToContents(0)
reportContentsWidget.setModel(reportContentsWidgetModel)

I tried I.e below, but it is not working:

reportContentsWidgetModel.setData(reportContentsWidgetModel.index(0, 0), QBrush(QColor(255, 0, 0)), Qt.BackgroundRole)
hashy
  • 175
  • 10
  • Are you asking how to set the background of the table headers? – musicamante Aug 30 '21 at 00:06
  • @musicamante first of all, the duplicate answer is in C++ and my question is in python, second yes, I wand the header columns to have a different background color – hashy Aug 30 '21 at 00:34
  • 1. I asked the question before it was marked as duplicate, and I did *not* mark it as such; 2. the fact that the suggested answer is in C++ doesn't change a lot: Qt functions work 99.9% of times in the same way as they do in Python; 3. as already suggested in your other post, you must be more clear with what you're asking, and in your question you didn't explain that what you wanted was to have *different* background colors for each section; 4. use `reportContentsWidgetModel.setHeaderData(column, Qt.Horizontal, QColor(255, 0, 0), Qt.BackgroundRole)`. – musicamante Aug 30 '21 at 00:52

0 Answers0