Python Forum
[PyQt] QTableWidget stylesheet error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] QTableWidget stylesheet error
#1
Hi,

I have a QTableWidget on my GUI and when I want to style the table headers, it does not style the top left section of the table.

The table is set-up as follows with the associated style sheet:

def create_table(gui):
    column_size = [100, 260, 150]
    columnLabels = ["A", "B", "C"]

    rowLabels = []
    for i in range(50):
        rowLabels.append(str(i+1))

    gui.table.setFixedWidth(sum(column_size)+34)
    gui.table.setFixedHeight(157)
    gui.table.verticalHeader().setMinimumSectionSize(23)
    gui.table.verticalHeader().setMaximumSectionSize(23)
    gui.table.verticalHeader().setDefaultSectionSize(23)
    gui.table.setHorizontalHeaderLabels(columnLabels)
    gui.table.setVerticalHeaderLabels(rowLabels)
    gui.table.verticalHeader().setVisible(True)
    gui.table.setFont(generalFont)

    cnt = 0
    for column in column_size:
        gui.table.setColumnWidth(cnt, column)
        cnt += 1

    gui.table.setStyleSheet("QHeaderView::section {background-color: rgba(100, 100, 100, 255); color: rgba(200, 200, 200, 255);}")
The headers are correctly styled but the square in the top left corner above the row labels is not styled.
Can someone help me with this?
Reply
#2
Well had you provided a MRE/MUC (Minimal Reproducible Example/Minimal Usable Code) you would have gotten a much quicker answer since all one, trying to do the favor you are requesting, would have had to do is copy/paste and run your code to see what you might have done wrong or to help you fix what is going wrong. So can you augment the above and make an MRE/MUC?
Reply
#3
I understand. See below the MRE.

from PySide2.QtGui import QFont
from PySide2.QtWidgets import *
import sys

class MainWindow(QDialog):
    def __init__(self):
        self.generalFont = QFont('Calibri', 10)

        super(MainWindow, self).__init__()
        self.resize(600, 300)
        self.layout = QGridLayout()
        self.layout.setSpacing(5)

        self.table_group = QFrame()
        self.table = QTableWidget()
        table_box = self.create_table_box()
        self.table_group.setLayout(table_box)
        self.construct_table()

        self.layout.addWidget(self.table_group, 0, 0)
        self.setLayout(self.layout)

    def construct_table(self):
        column_size = [100, 260, 150]
        column_labels = ["A", "B", "C"]

        rowLabels = []
        for i in range(50):
            rowLabels.append(str(i + 1))

        self.table.setRowCount(10)
        self.table.setColumnCount(len(column_size))

        self.table.setFixedWidth(sum(column_size) + 34)
        self.table.setFixedHeight(157)
        self.table.verticalHeader().setMinimumSectionSize(23)
        self.table.verticalHeader().setMaximumSectionSize(23)
        self.table.verticalHeader().setDefaultSectionSize(23)
        self.table.setHorizontalHeaderLabels(column_labels)
        self.table.setVerticalHeaderLabels(rowLabels)
        self.table.verticalHeader().setVisible(True)
        self.table.setFont(self.generalFont)

        cnt = 0
        for column in column_size:
            self.table.setColumnWidth(cnt, column)
            cnt += 1

        self.table.setStyleSheet(
            "QHeaderView::section {background-color: rgb(100, 100, 100); color: rgb(200, 200, 200);}")

    def create_table_box(self):
        table_box = QGridLayout()
        table_box.addWidget(self.table, 0, 0)

        return table_box

if __name__ == "__main__":
    app = QApplication(sys.argv)

    QApplication.setStyle(QStyleFactory.create('Fusion'))

    window = MainWindow()
    window.show()

    sys.exit(app.exec_())
Reply
#4
Okay while I found out what it is you need to reference I did not find how to reference it but I am sure that if you dig hard enough (and sometimes you need an industrial sized backhoe) you can find the how -- but the object is called the QTableCornerButton (aka its a button not a header element). I tried the obvious which is shown below but that had no effect. Other than that I cleaned up your code some (but it still is not very python-qt5-ish) and explained why I changed what I did. I hope these help you move in a positive direction and help you track down that elusive reference to the QTableCornerButton
from PySide2.QtGui     import QFont
from PySide2.QtWidgets import QApplication, QDialog, QFrame, QTableWidget, QGridLayout

# No longer needed
# import sys
 
class MainWindow(QDialog):
    def __init__(self):
      # Do not use Super( ) unless you fully understand the 3 issues
      # that need to be handled within your code to implement it 
      # correctly -- further unless you need it for that rather rare
      # case it was designed for you are creating more issues for no
      # reason since you do not have the rare issue
      # super(MainWindow, self).__init__()
        QDialog.__init__(self)
      # Do you really need a Class Font as it is only used once
      # self.ClassFont = QFont('Calibri', 10)
        self.resize(600, 300)
        # -------
        self.table = QTableWidget()
        # -------
      # Do not create a function for 2 lines of non-repeated code
      # table_box = self.create_table_box()
        table_box = QGridLayout()
        table_box.addWidget(self.table, 0, 0)
        # -------
        self.table_group = QFrame()
        self.table_group.setLayout(table_box)
        # -------
      # You really do not need a Grid Layout here a QVBoxLayout and/or QHBoxLayout
      # would have worked just as well with less overhead
        self.layout = QGridLayout()
      # You are only using 1 Column and 1 Row of the Grid Layout so why are you
      # setting the Spacing this does not affect your Table Widget's Columns
        self.layout.setSpacing(5)
        self.layout.addWidget(self.table_group, 0, 0)
        # -------
        self.setLayout(self.layout)
        # -------
        self.construct_table()
 
    def construct_table(self):
        column_size = [100, 260, 150]
        column_labels = ["A", "B", "C"]
 
        rowLabels = []
        for i in range(50):
            rowLabels.append(str(i + 1))
 
        self.table.setRowCount(10)
        self.table.setColumnCount(len(column_size))
 
        self.table.setFixedWidth(sum(column_size) + 34)
        self.table.setFixedHeight(157)

        self.table.verticalHeader().setMinimumSectionSize(23)
        self.table.verticalHeader().setMaximumSectionSize(23)
        self.table.verticalHeader().setDefaultSectionSize(23)

        self.table.setHorizontalHeaderLabels(column_labels)
        self.table.setVerticalHeaderLabels(rowLabels)
        self.table.verticalHeader().setVisible(True)
      # If you do not need a Class Level variable do not create it
      # self.table.setFont(self.ClassFont)
        self.table.setFont(QFont('Calibri', 10))
 
        cnt = 0
        for column in column_size:
            self.table.setColumnWidth(cnt, column)
            cnt += 1
 
        self.table.setStyleSheet("QHeaderView::section {background-color: rgb(100, 100, 100); color: rgb(200, 200, 200);}")
      # Again while this does not seem to do anything it does not crash either so maybe its a peek into what you need to do
        self.table.setStyleSheet("QTableCornerButton::section {background-color: rgb(100, 100, 100); color: rgb(200, 200, 200);}")

if __name__ == '__main__':
  # First if you are not using Command Line arguments do not reference them
  # and if you do use Command Line arguments use the argparser library instead
  # it handles them much more cleanly and intuitively
  # app = QApplication(sys.argv)
    MainEvntHndlr = QApplication([])
  # When setting the Application Style be sure to reference the Application
  # object you created as well use the method correctly
  # QApplication.setStyle(QStyleFactory.create('Fusion'))
    MainEvntHndlr.setStyle('Fusion')

    MainApp = MainWindow()
    MainApp.show()

  # This is how to do this in Qt5 your version was Qt4
  # sys.exit(app.exec_())
    MainEvntHndlr.exec()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] QTreeView, StyleSheet resizes last column malonn 2 862 Sep-15-2023, 03:50 PM
Last Post: malonn
  [PyQt] QTableWidget print problem JokerSob 8 4,686 Jan-28-2022, 06:08 PM
Last Post: Axel_Erfurt
  [PyQt] How do I display the DB table I will choose from the QComboBox in QTableWidget JokerSob 2 2,273 Aug-05-2021, 03:00 PM
Last Post: JokerSob
  Displaying database info in QTableWidget thewolf 6 5,183 Apr-03-2021, 02:49 PM
Last Post: thewolf
  [PyQt] Help: check content of combobox in horizontal header of QTableWidget mart79 1 3,269 Jul-26-2020, 06:18 PM
Last Post: deanhystad
  [PyQt] Add validation (regex) to QTableWidget cells mart79 0 2,690 May-05-2020, 12:51 PM
Last Post: mart79
  [PyQt] Pyqt5: How do you make a button that adds new row with data to a Qtablewidget YoshikageKira 6 6,878 Jan-02-2020, 04:32 PM
Last Post: Denni
  [PyQt] QTableWidget cell validation littleGreenDude 1 7,617 Jan-18-2019, 07:44 PM
Last Post: littleGreenDude
  Printing QTableWidget to an DIN A4 page Mady 2 7,145 Dec-18-2018, 06:56 PM
Last Post: Axel_Erfurt
  Cannot Import stylesheet BG? mekha 1 2,233 Aug-08-2018, 04:49 AM
Last Post: mekha

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020