Python Forum
[PyQt] How to reference QTreeView QHeaderView setResizeMode
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] How to reference QTreeView QHeaderView setResizeMode
#1
The QTreeView documentation states that it contains a QHeaderView object called "header"
https://het.as.utexas.edu/HET/Software/P...eview.html

The QHeaderView documentation states that it contains a method called "setResizeMode"
https://het.as.utexas.edu/HET/Software/P...rview.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_())
Reply
#2
Here https://doc.qt.io/qt-5/qheaderview-obsol...ResizeMode
Quote:void QHeaderView::setResizeMode(QHeaderView::ResizeMode mode)

This function is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.

Use setSectionResizeMode instead.

See also resizeMode() and setSectionResizeMode().
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] QTreeView, StyleSheet resizes last column malonn 2 861 Sep-15-2023, 03:50 PM
Last Post: malonn
  [PyQt] Changing a column header in QTreeView DrakeSoft 4 3,488 Jun-25-2022, 09:11 AM
Last Post: DrakeSoft
  [PyQt] QTreeView branches and their clickable area not coinciding abstracted 0 3,680 Mar-17-2017, 02:28 AM
Last Post: abstracted

Forum Jump:

User Panel Messages

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