Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. ResizeSection of horizontalHeader doesn't resize a section when setStyleSheet is set
Qt 6.11 is out! See what's new in the release blog

ResizeSection of horizontalHeader doesn't resize a section when setStyleSheet is set

Scheduled Pinned Locked Moved Unsolved Qt for Python
5 Posts 2 Posters 849 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    George55978
    wrote on last edited by
    #1

    I can't figure out what is wrong.
    It seems like resizeSection of horizontalHeader in QTableview doesn't make any effect when some else setStyleSheet is set. Here is my code:

    import sys
    from PyQt6.QtSql import QSqlTableModel
    from PyQt6.QtWidgets import (
                                 QApplication,   
                                 QMainWindow,
                                 QVBoxLayout,
                                 QWidget,
                                 QTableView,
                               
                                )
    
    from db import make_check_db_connection
    
    class TestWindow(QMainWindow):
        def __init__(self, parent=None):
            super().__init__(parent)
            
            layuot = QVBoxLayout()
    
            self.model = QSqlTableModel(db=make_check_db_connection())
            self.model.setTable("Rests")
            
            self.table = QTableView()
            self.table.setModel(self.model)
            self.model.select()
                   
            # self.setStyleSheet("QMainWindow {background-color: yellow;}")
            self.table.horizontalHeader().setStyleSheet("QHeaderView::section { background-color: green;}")
            
            self.table.horizontalHeader().resizeSection(1, 300)
            layuot.addWidget(self.table)
            widget = QWidget()
            widget.setLayout(layuot)
            self.setCentralWidget(widget)
            self.show()
    app = QApplication(sys.argv)
    window = TestWindow()
    window.show()
    app.exec()
    

    1.jpg
    img1
    2.jpg
    Img2
    The results for commented and uncommented

    self.setStyleSheet("QMainWindow {background-color: yellow;}")
    

    are on Img1 and Img2 respectively.
    So for uncommented case the width of 1st section remains unchanged.
    Ideally I would like to set QSS at the TestWindow level for other widgets but it also breaks resizeSection.
    I would appreciate any help.

    Christian EhrlicherC 1 Reply Last reply
    0
    • G George55978

      I can't figure out what is wrong.
      It seems like resizeSection of horizontalHeader in QTableview doesn't make any effect when some else setStyleSheet is set. Here is my code:

      import sys
      from PyQt6.QtSql import QSqlTableModel
      from PyQt6.QtWidgets import (
                                   QApplication,   
                                   QMainWindow,
                                   QVBoxLayout,
                                   QWidget,
                                   QTableView,
                                 
                                  )
      
      from db import make_check_db_connection
      
      class TestWindow(QMainWindow):
          def __init__(self, parent=None):
              super().__init__(parent)
              
              layuot = QVBoxLayout()
      
              self.model = QSqlTableModel(db=make_check_db_connection())
              self.model.setTable("Rests")
              
              self.table = QTableView()
              self.table.setModel(self.model)
              self.model.select()
                     
              # self.setStyleSheet("QMainWindow {background-color: yellow;}")
              self.table.horizontalHeader().setStyleSheet("QHeaderView::section { background-color: green;}")
              
              self.table.horizontalHeader().resizeSection(1, 300)
              layuot.addWidget(self.table)
              widget = QWidget()
              widget.setLayout(layuot)
              self.setCentralWidget(widget)
              self.show()
      app = QApplication(sys.argv)
      window = TestWindow()
      window.show()
      app.exec()
      

      1.jpg
      img1
      2.jpg
      Img2
      The results for commented and uncommented

      self.setStyleSheet("QMainWindow {background-color: yellow;}")
      

      are on Img1 and Img2 respectively.
      So for uncommented case the width of 1st section remains unchanged.
      Ideally I would like to set QSS at the TestWindow level for other widgets but it also breaks resizeSection.
      I would appreciate any help.

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      What Qt version do you use?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      G 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        What Qt version do you use?

        G Offline
        G Offline
        George55978
        wrote on last edited by
        #3

        @Christian-Ehrlicher said in ResizeSection of horizontalHeader doesn't resize a section when setStyleSheet is set:

        What Qt version do you use?

        Name: PyQt6
        Version: 6.6.1

        Christian EhrlicherC 1 Reply Last reply
        0
        • G George55978

          @Christian-Ehrlicher said in ResizeSection of horizontalHeader doesn't resize a section when setStyleSheet is set:

          What Qt version do you use?

          Name: PyQt6
          Version: 6.6.1

          Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          See https://bugreports.qt.io/browse/QTBUG-122109

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          G 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            See https://bugreports.qt.io/browse/QTBUG-122109

            G Offline
            G Offline
            George55978
            wrote on last edited by
            #5

            @Christian-Ehrlicher
            got it, thanks

            1 Reply Last reply
            0
            • SGaistS SGaist moved this topic from General and Desktop on

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved