How to generically set the Header Item Data
-
I am basically a newbie to Qt5 but a long time experienced software engineer I have in the last couple of weeks been able to figure out quite a few things about Qt5 but this one troubles me since I will eventually have numerous columns and I am having to set this information individually for each column using a separate "container" which seems counter-intuitive. This is what I have thus far -- what I am trying to figure out is how to have a single HdrItem and apply it to each of the HeaderLabels -- this gets added into a CenterPane = QSplitter(Qt.Horizontal, self) once its created and that works fine but having to do this for each and every column header seems rather cumbersome which is why it seems counter-intuitive to how Qt5 works. Note if I do not do it this way it complains about adding duplicates and only the first column header gets the formatting
//class ItemDsplyr(QTreeView): def __init__(self, parent): QTreeView.__init__(self, parent) HdrItem0 = QStandardItem() HdrItem1 = QStandardItem() HdrItem2 = QStandardItem() font = QFont() font.setBold(True) font.setPointSize(10) HdrItem0.setFont(font) HdrItem1.setFont(font) HdrItem2.setFont(font) brush = QBrush() brush.setColor(Qt.blue) brush.setStyle(Qt.SolidPattern) HdrItem0.setBackground(brush) HdrItem0.setForeground(brush) HdrItem1.setForeground(brush) HdrItem2.setForeground(brush) self.model = QStandardItemModel(0, 3) self.model.setHorizontalHeaderItem(0, HdrItem0) self.model.setHorizontalHeaderItem(1, HdrItem1) self.model.setHorizontalHeaderItem(2, HdrItem2) self.model.setHorizontalHeaderLabels(['Column1', 'Column2', 'Column3']) self.setModel(self.model) self.clicked.connect(self.itemSingleClicked)
-
No I have not as that seemed a bit excessive just to manipulate that bit of information but if there is no other way to manipulate that information then perhaps that is the only way. I am hoping for a much cleaner more intuitive approach as I feel that bit of information should be manipulatable without having to override a base class but then I could be wrong on that. Thanks for that suggestion though
-
okay @KillerSmath it appears you are correct and the QStandardItemModel class has to be overridden got help from someone on another forum and this was the solution:
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): QTreeView.__init__(self) self.model = CustomItemModel(0, 3) self.model.setHorizontalHeaderLabels(['Column1', 'Column2', 'Column3']) self.setModel(self.model) self.clicked.connect(self.itemSingleClicked) def itemSingleClicked(self, index): Item = self.selectedIndexes()[0] ItemVal = Item.model().itemFromIndex(index).text() print("Item Clicked:",ItemVal) def SetContent(self): self.model.setRowCount(0) ItmRecSet = [ {'CatgryName':'Cat-1', 'GroupName':'Run-Grp-1', 'ItemName':'Run-Itm-1'}, {'CatgryName':'Cat-1', 'GroupName':'Run-Grp-1', 'ItemName':'Run-Itm-2'}, {'CatgryName':'Cat-1', 'GroupName':'Run-Grp-1', 'ItemName':'Run-Itm-3'} ] for Item in ItmRecSet: ItmNam = QStandardItem(Item['ItemName']) CatNam = QStandardItem(Item['CatgryName']) GrpNam = QStandardItem(Item['GroupName']) self.model.appendRow([CatNam, GrpNam, ItmNam])