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. [pyqt5 / python 3.7] How to reference QTreeView QHeaderView setResizeMode
Forum Updated to NodeBB v4.3 + New Features

[pyqt5 / python 3.7] How to reference QTreeView QHeaderView setResizeMode

Scheduled Pinned Locked Moved Solved Qt for Python
2 Posts 2 Posters 1.0k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    Denni
    wrote on 11 Jun 2019, 20:29 last edited by Denni 6 Nov 2019, 20:35
    #1

    The QTreeView documentation states that it contains a QHeaderView object called "header"
    https://het.as.utexas.edu/HET/Software/PyQt/qtreeview.html

    The QHeaderView documentation states that it contains a method called "setResizeMode"
    https://het.as.utexas.edu/HET/Software/PyQt/qheaderview.html

    However when I try to reference that method from in code I get the following error:

         self.header().setResizeMode(1, QHeaderView.Stretch)              
         AttributeError: 'QHeaderView' object has no attribute 'setResizeMode'
    

    The code I am including has the above reference remarked out so that it works and you can see what the end product looks like however if you remove the comment marker from that line you will get the above error when you run it. As denoted within the code this is how I would expect it to be reference and the closest that I got rendering that I can tell because it actually references the QHeaderView class -- the others do not get that far but they are also included for example completeness just in case

    import sys
    
    from PyQt5.QtCore    import *
    from PyQt5.QtGui     import *
    from PyQt5.QtWidgets import *
    
    class CustomItemModel(QStandardItemModel):
        def headerData(self, section, orientation, role):
            if role == Qt.ForegroundRole:
                brush = QBrush()
                brush.setColor(Qt.blue)
                brush.setStyle(Qt.SolidPattern)
                return brush
                  
            elif role == Qt.BackgroundRole:
                brush = QBrush()
                brush.setColor(Qt.yellow)
                brush.setStyle(Qt.SolidPattern)
                return brush
              
            elif role == Qt.FontRole:
                font = QFont()
                font.setBold(True)
                font.setPointSize(10)
                return font
                  
            return super().headerData(section, orientation, role)
    
    class ItemDsplyr(QTreeView):
        def __init__(self, CentrPane):
            QTreeView.__init__(self, CentrPane)
            self.CntrPane = CentrPane
    
            self.model = CustomItemModel(0, 4)
    # For some reason the 1st Column's Data is Idented so for now
    # I added a dummy column to prevent this from happening until
    # I can figure out why its happening
            self.model.setHorizontalHeaderLabels(['', 'ItemName', 'Value', 'Units'])
    # Center the column header "Value"
            self.model.setHeaderData(2, Qt.Horizontal, Qt.AlignCenter, Qt.TextAlignmentRole)
    
            self.setModel(self.model)
    
    # This got the closest it that it told me that the QHeaderView
    # does not have a "setResizeMode" attribute even though the
    # documentation states that it should -- this also appears
    # (per the documentation) as the correct way to reference this
    # method
    #        self.header().setResizeMode(1, QHeaderView.Stretch)
    
    # Still I have also tried all of these and none of them work
    #        self.Header.setResizeMode(1, QHeaderView.Stretch)
    #        self.Header().setResizeMode(1, QHeaderView.Stretch)
    #        self.header.setResizeMode(1, QHeaderView.Stretch)
    
    
    # This does not appear to be working as of yet although I get no error
            self.setColumnWidth(0, 1)
    # This appears to be working
            self.resizeColumnToContents(2)
    # This might work if I can set column 1 to Stretch
            self.resizeColumnToContents(3)
    
        @property
        def CntrPane(self):
            return self.__parent
    
        @CntrPane.setter
        def CntrPane(self, value):
            self.__parent = value
    
        def SetContent(self):
            self.model.setRowCount(0)
    
            ItmRecSet = [
                {'ItemName':'Run-Itm-1', 'Value':'2',  'Units':'Units'},
                {'ItemName':'Run-Itm-2', 'Value':'1',  'Units':'Units'},
                {'ItemName':'Run-Itm-3', 'Value':'1',  'Units':'Units'},
                {'ItemName':'Run-Itm-1', 'Value':'10', 'Units':'Units'},
                {'ItemName':'Run-Itm-2', 'Value':'50', 'Units':'Units'},
                {'ItemName':'Run-Itm-3', 'Value':'0',  'Units':'Units'},
                {'ItemName':'Run-Itm-1', 'Value':'0',  'Units':'Clock Cycles'},
                {'ItemName':'Run-Itm-2', 'Value':'0',  'Units':'Units'},
                {'ItemName':'Run-Itm-3', 'Value':'0',  'Units':'Units'},
                ]
    
            for Item in ItmRecSet:
                blnkVal = QStandardItem('')
    
                ItmNam = QStandardItem(Item['ItemName'])
                ItmNam.setTextAlignment(Qt.AlignLeft)
                ItmNam.isEditable = False
    
                ItmVal = QStandardItem(Item['Value'])
                ItmVal.setTextAlignment(Qt.AlignCenter)
    
                ItmUnt = QStandardItem((Item['Units']))
                ItmUnt.setTextAlignment(Qt.AlignLeft)
                ItmUnt.isEditable = False
    
                self.model.appendRow([blnkVal, ItmNam, ItmVal, ItmUnt])
    
    class CenterPanel(QWidget):
        def __init__(self, MainWin):
            QWidget.__init__(self)
            self.MainWin = MainWin
    
            self.ItemDsply = ItemDsplyr(self)
            self.ItemDsply.SetContent()
    
            CntrPane = QSplitter(Qt.Horizontal, self)
            CntrPane.addWidget(QTextEdit())
            CntrPane.addWidget(self.ItemDsply)
            CntrPane.setSizes([50,200])
    
            hbox = QHBoxLayout(self)
            hbox.addWidget(CntrPane)
    
            self.setLayout(hbox)
    
        @property
        def MainWin(self):
            return self.__parent
    
        @MainWin.setter
        def MainWin(self, value):
            self.__parent = value
    
    class MainWin(QMainWindow):
        def __init__(self, parent=None):
            super(MainWin, self).__init__(parent)
            
            self.left   = 100
            self.top    = 100
            self.width  = 700
            self.height = 600
    
            self.setWindowTitle('Main Window')
            self.setGeometry(self.left, self.top, self.width, self.height)
    
            self.CenterPane = CenterPanel(self)
            self.setCentralWidget(self.CenterPane)
    
    
    if __name__ == "__main__":
        MainProg = QApplication([])
        GUI = MainWin()
        GUI.show()
        sys.exit(MainProg.exec_())
    

    madness... is like gravity, all takes is a little... push -- like from an unsolvable bug

    J 1 Reply Last reply 12 Jun 2019, 05:50
    0
    • D Denni
      11 Jun 2019, 20:29

      The QTreeView documentation states that it contains a QHeaderView object called "header"
      https://het.as.utexas.edu/HET/Software/PyQt/qtreeview.html

      The QHeaderView documentation states that it contains a method called "setResizeMode"
      https://het.as.utexas.edu/HET/Software/PyQt/qheaderview.html

      However when I try to reference that method from in code I get the following error:

           self.header().setResizeMode(1, QHeaderView.Stretch)              
           AttributeError: 'QHeaderView' object has no attribute 'setResizeMode'
      

      The code I am including has the above reference remarked out so that it works and you can see what the end product looks like however if you remove the comment marker from that line you will get the above error when you run it. As denoted within the code this is how I would expect it to be reference and the closest that I got rendering that I can tell because it actually references the QHeaderView class -- the others do not get that far but they are also included for example completeness just in case

      import sys
      
      from PyQt5.QtCore    import *
      from PyQt5.QtGui     import *
      from PyQt5.QtWidgets import *
      
      class CustomItemModel(QStandardItemModel):
          def headerData(self, section, orientation, role):
              if role == Qt.ForegroundRole:
                  brush = QBrush()
                  brush.setColor(Qt.blue)
                  brush.setStyle(Qt.SolidPattern)
                  return brush
                    
              elif role == Qt.BackgroundRole:
                  brush = QBrush()
                  brush.setColor(Qt.yellow)
                  brush.setStyle(Qt.SolidPattern)
                  return brush
                
              elif role == Qt.FontRole:
                  font = QFont()
                  font.setBold(True)
                  font.setPointSize(10)
                  return font
                    
              return super().headerData(section, orientation, role)
      
      class ItemDsplyr(QTreeView):
          def __init__(self, CentrPane):
              QTreeView.__init__(self, CentrPane)
              self.CntrPane = CentrPane
      
              self.model = CustomItemModel(0, 4)
      # For some reason the 1st Column's Data is Idented so for now
      # I added a dummy column to prevent this from happening until
      # I can figure out why its happening
              self.model.setHorizontalHeaderLabels(['', 'ItemName', 'Value', 'Units'])
      # Center the column header "Value"
              self.model.setHeaderData(2, Qt.Horizontal, Qt.AlignCenter, Qt.TextAlignmentRole)
      
              self.setModel(self.model)
      
      # This got the closest it that it told me that the QHeaderView
      # does not have a "setResizeMode" attribute even though the
      # documentation states that it should -- this also appears
      # (per the documentation) as the correct way to reference this
      # method
      #        self.header().setResizeMode(1, QHeaderView.Stretch)
      
      # Still I have also tried all of these and none of them work
      #        self.Header.setResizeMode(1, QHeaderView.Stretch)
      #        self.Header().setResizeMode(1, QHeaderView.Stretch)
      #        self.header.setResizeMode(1, QHeaderView.Stretch)
      
      
      # This does not appear to be working as of yet although I get no error
              self.setColumnWidth(0, 1)
      # This appears to be working
              self.resizeColumnToContents(2)
      # This might work if I can set column 1 to Stretch
              self.resizeColumnToContents(3)
      
          @property
          def CntrPane(self):
              return self.__parent
      
          @CntrPane.setter
          def CntrPane(self, value):
              self.__parent = value
      
          def SetContent(self):
              self.model.setRowCount(0)
      
              ItmRecSet = [
                  {'ItemName':'Run-Itm-1', 'Value':'2',  'Units':'Units'},
                  {'ItemName':'Run-Itm-2', 'Value':'1',  'Units':'Units'},
                  {'ItemName':'Run-Itm-3', 'Value':'1',  'Units':'Units'},
                  {'ItemName':'Run-Itm-1', 'Value':'10', 'Units':'Units'},
                  {'ItemName':'Run-Itm-2', 'Value':'50', 'Units':'Units'},
                  {'ItemName':'Run-Itm-3', 'Value':'0',  'Units':'Units'},
                  {'ItemName':'Run-Itm-1', 'Value':'0',  'Units':'Clock Cycles'},
                  {'ItemName':'Run-Itm-2', 'Value':'0',  'Units':'Units'},
                  {'ItemName':'Run-Itm-3', 'Value':'0',  'Units':'Units'},
                  ]
      
              for Item in ItmRecSet:
                  blnkVal = QStandardItem('')
      
                  ItmNam = QStandardItem(Item['ItemName'])
                  ItmNam.setTextAlignment(Qt.AlignLeft)
                  ItmNam.isEditable = False
      
                  ItmVal = QStandardItem(Item['Value'])
                  ItmVal.setTextAlignment(Qt.AlignCenter)
      
                  ItmUnt = QStandardItem((Item['Units']))
                  ItmUnt.setTextAlignment(Qt.AlignLeft)
                  ItmUnt.isEditable = False
      
                  self.model.appendRow([blnkVal, ItmNam, ItmVal, ItmUnt])
      
      class CenterPanel(QWidget):
          def __init__(self, MainWin):
              QWidget.__init__(self)
              self.MainWin = MainWin
      
              self.ItemDsply = ItemDsplyr(self)
              self.ItemDsply.SetContent()
      
              CntrPane = QSplitter(Qt.Horizontal, self)
              CntrPane.addWidget(QTextEdit())
              CntrPane.addWidget(self.ItemDsply)
              CntrPane.setSizes([50,200])
      
              hbox = QHBoxLayout(self)
              hbox.addWidget(CntrPane)
      
              self.setLayout(hbox)
      
          @property
          def MainWin(self):
              return self.__parent
      
          @MainWin.setter
          def MainWin(self, value):
              self.__parent = value
      
      class MainWin(QMainWindow):
          def __init__(self, parent=None):
              super(MainWin, self).__init__(parent)
              
              self.left   = 100
              self.top    = 100
              self.width  = 700
              self.height = 600
      
              self.setWindowTitle('Main Window')
              self.setGeometry(self.left, self.top, self.width, self.height)
      
              self.CenterPane = CenterPanel(self)
              self.setCentralWidget(self.CenterPane)
      
      
      if __name__ == "__main__":
          MainProg = QApplication([])
          GUI = MainWin()
          GUI.show()
          sys.exit(MainProg.exec_())
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 12 Jun 2019, 05:50 last edited by
      #2

      @Denni This method is obsoleted and probably not available in PyQt anymore for that reason (just an assumption).
      See https://doc.qt.io/qt-5/qheaderview-obsolete.html#setResizeMode
      The advice is to use https://doc.qt.io/qt-5/qheaderview.html#setSectionResizeMode

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      1/2

      11 Jun 2019, 20:29

      • Login

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