[pyqt5 / python 3.7] How to reference QTreeView QHeaderView setResizeMode
-
The QTreeView documentation states that it contains a QHeaderView object called "header"
https://het.as.utexas.edu/HET/Software/PyQt/qtreeview.htmlThe QHeaderView documentation states that it contains a method called "setResizeMode"
https://het.as.utexas.edu/HET/Software/PyQt/qheaderview.htmlHowever 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_())
-
@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