Python Forum
[PyQt] How to generically set the Header Item Data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] How to generically set the Header Item Data
#6
Hey Denni, I think overridding your model is right way to go here: what you're looking to do is change the presentation of your data, which is exactly what the model in model-view is for. This avoids creating needless objects, and allows you to easily update the styles on the fly.

In the example below the column styles are stored in two dictionaries, but you can of course change them out for anything you like.

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui  import *
from PyQt5.QtWidgets import *


brush = QBrush()
brush.setColor(Qt.blue)
brush.setStyle(Qt.SolidPattern)

COLUMN_FOREGROUND_STYLES = {
    1: brush,
    2: brush,
    3: brush,
}

COLUMN_BACKGROUND_STYLES = {
    0: brush,
}


class CustomItemModel(QStandardItemModel):

    def headerData(self, section, orientation, role):
        if role == Qt.ForegroundRole:
            return COLUMN_FOREGROUND_STYLES.get(section)
            
        elif role == Qt.BackgroundRole:
            return COLUMN_BACKGROUND_STYLES.get(section)
        
        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])
 
class CenterPane(QWidget):
    def __init__(self, parent):
        QWidget.__init__(self)
 
        self.MainWin = parent
 
        self.ItemDsply = ItemDsplyr()
        self.ItemDsply.SetContent()
 
        CntrPane = QSplitter(Qt.Horizontal, self)
        CntrPane.addWidget(QTextEdit())
        CntrPane.addWidget(self.ItemDsply)
        CntrPane.setSizes([75,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 Window(QMainWindow):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.setCentralWidget(CenterPane(self))
 
if __name__ == "__main__":
    qApp = QApplication([])
    GUI = Window()
    GUI.show()
    sys.exit(qApp.exec_())
Let me know if this is way off the mark.
Reply


Messages In This Thread
RE: How to generically set the Header Item Data - by mfitzp - Jun-04-2019, 05:14 PM

Forum Jump:

User Panel Messages

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