Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to generically set the Header Item Data

How to generically set the Header Item Data

Scheduled Pinned Locked Moved Solved Qt for Python
4 Posts 2 Posters 1.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • DenniD Offline
    DenniD Offline
    Denni
    wrote on last edited by
    #1

    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)
    
    

    madness... is like gravity, all takes is a little... push -- like from an unsolvable bug

    KillerSmathK 1 Reply Last reply
    0
    • DenniD Denni

      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)
      
      
      KillerSmathK Offline
      KillerSmathK Offline
      KillerSmath
      wrote on last edited by
      #2

      Hi @Denni

      Have you tried to override a QStandardItemModel class ? It will allow you to custom a way to populate your Model and return specific data like for example your HeaderData.

      @Computer Science Student - Brazil
      Web Developer and Researcher
      “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

      1 Reply Last reply
      0
      • DenniD Offline
        DenniD Offline
        Denni
        wrote on last edited by
        #3

        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

        madness... is like gravity, all takes is a little... push -- like from an unsolvable bug

        1 Reply Last reply
        0
        • DenniD Offline
          DenniD Offline
          Denni
          wrote on last edited by
          #4

          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])
          

          madness... is like gravity, all takes is a little... push -- like from an unsolvable bug

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved