Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [PyQt] How to reference QTreeView QHeaderView setResizeMode
The QTreeView documentation states that it contains a QHeaderView object called "header"

The QHeaderView documentation states that it contains a method called "setResizeMode"

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()
            return brush
        elif role == Qt.BackgroundRole:
            brush = QBrush()
            return brush
        elif role == Qt.FontRole:
            font = QFont()
            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)


# 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
# This might work if I can set column 1 to Stretch

    def CntrPane(self):
        return self.__parent

    def CntrPane(self, value):
        self.__parent = value

    def SetContent(self):

        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.isEditable = False

            ItmVal = QStandardItem(Item['Value'])

            ItmUnt = QStandardItem((Item['Units']))
            ItmUnt.isEditable = False

            self.model.appendRow([blnkVal, ItmNam, ItmVal, ItmUnt])

class CenterPanel(QWidget):
    def __init__(self, MainWin):
        self.MainWin = MainWin

        self.ItemDsply = ItemDsplyr(self)

        CntrPane = QSplitter(Qt.Horizontal, self)

        hbox = QHBoxLayout(self)


    def MainWin(self):
        return self.__parent

    def MainWin(self, value):
        self.__parent = value

class MainWin(QMainWindow):
    def __init__(self, parent=None):
        super(MainWin, self).__init__(parent)
        self.left   = 100    = 100
        self.width  = 700
        self.height = 600

        self.setWindowTitle('Main Window')
        self.setGeometry(self.left,, self.width, self.height)

        self.CenterPane = CenterPanel(self)

if __name__ == "__main__":
    MainProg = QApplication([])
    GUI = MainWin()
silly American kah-nig-hit
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().
Denni likes this post

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [PyQt] QTreeView branches and their clickable area not coinciding abstracted 0 1,342 Mar-17-2017, 02:28 AM
Last Post: abstracted

Forum Jump:

Users browsing this thread: 1 Guest(s)