Python Forum
[PyQt] QTableWidget stylesheet error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] QTableWidget stylesheet error
#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


Messages In This Thread
QTableWidget stylesheet error - by mart79 - Feb-25-2020, 03:02 PM
RE: QTableWidget stylesheet error - by Denni - Feb-25-2020, 04:16 PM
RE: QTableWidget stylesheet error - by mart79 - Feb-26-2020, 06:45 AM
RE: QTableWidget stylesheet error - by Denni - Feb-26-2020, 04:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] QTreeView, StyleSheet resizes last column malonn 2 985 Sep-15-2023, 03:50 PM
Last Post: malonn
  [PyQt] QTableWidget print problem JokerSob 8 4,850 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,356 Aug-05-2021, 03:00 PM
Last Post: JokerSob
  Displaying database info in QTableWidget thewolf 6 5,372 Apr-03-2021, 02:49 PM
Last Post: thewolf
  [PyQt] Help: check content of combobox in horizontal header of QTableWidget mart79 1 3,414 Jul-26-2020, 06:18 PM
Last Post: deanhystad
  [PyQt] Add validation (regex) to QTableWidget cells mart79 0 2,781 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 7,119 Jan-02-2020, 04:32 PM
Last Post: Denni
  [PyQt] QTableWidget cell validation littleGreenDude 1 7,728 Jan-18-2019, 07:44 PM
Last Post: littleGreenDude
  Printing QTableWidget to an DIN A4 page Mady 2 7,274 Dec-18-2018, 06:56 PM
Last Post: Axel_Erfurt
  Cannot Import stylesheet BG? mekha 1 2,298 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