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. PyQt6 - Color different keywords in QTableWidget cell text
Forum Updated to NodeBB v4.3 + New Features

PyQt6 - Color different keywords in QTableWidget cell text

Scheduled Pinned Locked Moved Unsolved Qt for Python
4 Posts 2 Posters 1.5k Views
  • 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.
  • Z Offline
    Z Offline
    zeroalpha
    wrote on last edited by
    #1

    I need to change the color of a keyword if found inside the cell of a QTableWidget for PyQt6

    qtablewidget sample

    In my example: if '1' appears in a cell,
    change the color to RED

    So it looks like this:

    highlight matching word with different color

    import sys
    
    from PyQt6.QtWidgets import QApplication, QTableWidget, QTableWidgetItem
    
    data = {'col1': ['one','highlight only 1','test'],
            'col2': ['cool','4','um'],
            'col3': ['123','1','2']}
    
    class TableView(QTableWidget):
        def __init__(self, data, *args):
            QTableWidget.__init__(self, *args)
            self.data = data
            self.setData()
            self.resizeColumnsToContents()
            self.resizeRowsToContents()
    
        def setData(self):
            horHeaders = []
            for n, key in enumerate(sorted(self.data.keys())):
                horHeaders.append(key)
                for m, item in enumerate(self.data[key]):
                    newitem = QTableWidgetItem(item)
                    self.setItem(m, n, newitem)
            self.setHorizontalHeaderLabels(horHeaders)
    
    
    def main(args):
        app = QApplication(args)
        table = TableView(data, 3, 3)
        table.show()
        sys.exit(app.exec())
    
    
    if __name__ == "__main__":
        main(sys.argv)
    
    JonBJ 1 Reply Last reply
    0
    • Z zeroalpha

      I need to change the color of a keyword if found inside the cell of a QTableWidget for PyQt6

      qtablewidget sample

      In my example: if '1' appears in a cell,
      change the color to RED

      So it looks like this:

      highlight matching word with different color

      import sys
      
      from PyQt6.QtWidgets import QApplication, QTableWidget, QTableWidgetItem
      
      data = {'col1': ['one','highlight only 1','test'],
              'col2': ['cool','4','um'],
              'col3': ['123','1','2']}
      
      class TableView(QTableWidget):
          def __init__(self, data, *args):
              QTableWidget.__init__(self, *args)
              self.data = data
              self.setData()
              self.resizeColumnsToContents()
              self.resizeRowsToContents()
      
          def setData(self):
              horHeaders = []
              for n, key in enumerate(sorted(self.data.keys())):
                  horHeaders.append(key)
                  for m, item in enumerate(self.data[key]):
                      newitem = QTableWidgetItem(item)
                      self.setItem(m, n, newitem)
              self.setHorizontalHeaderLabels(horHeaders)
      
      
      def main(args):
          app = QApplication(args)
          table = TableView(data, 3, 3)
          table.show()
          sys.exit(app.exec())
      
      
      if __name__ == "__main__":
          main(sys.argv)
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @zeroalpha
      You show that you want to color substring 1 wherever it appears it any cell. Since QTableWidgetItems do not accept rich/HTML text you will need one of:

      • Use setCellWidget() to make every cell hold e.g. a QLabel or QTextEdit into which you put rich text with necessary coloring. Not recommended because you will have many cell widgets which is inefficient, plus you will iterate every item and alter its content to do the coloring/uncoloring.

      • Write a custom QStyledItemDelegate which outputs the HTML for the cell when called. Preferable.

      Z 1 Reply Last reply
      2
      • JonBJ JonB

        @zeroalpha
        You show that you want to color substring 1 wherever it appears it any cell. Since QTableWidgetItems do not accept rich/HTML text you will need one of:

        • Use setCellWidget() to make every cell hold e.g. a QLabel or QTextEdit into which you put rich text with necessary coloring. Not recommended because you will have many cell widgets which is inefficient, plus you will iterate every item and alter its content to do the coloring/uncoloring.

        • Write a custom QStyledItemDelegate which outputs the HTML for the cell when called. Preferable.

        Z Offline
        Z Offline
        zeroalpha
        wrote on last edited by
        #3

        @JonB Thank you. Is there a simple example somewhere of how to use the QStyledItemDelegate which outputs the HTML for the cell

        JonBJ 1 Reply Last reply
        0
        • Z zeroalpha

          @JonB Thank you. Is there a simple example somewhere of how to use the QStyledItemDelegate which outputs the HTML for the cell

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @zeroalpha
          I would Google for qstyleditemdelegate html or qstyleditemdelegate rich text. What about Displaying QTableWidgetItem's text with different colors via a QStyledItemDelegate or How to make item view render rich (html) text in Qt ?

          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