title: How to highlight a words in QTableWidget from a Searchlist
body: <p>I want to search a QTableWidget-Table by a list of words, if they've been found i want them to bee highlighted.</p>
  
  <p>I tried to modify the code from <a href="https://stackoverflow.com/questions/51613638/highlight-search-results-in-qtablewidgetselect-and-highlight-that-text-or-chara">here</a> so the table is beeing searched by a list of words, not just one. Unfortunatly my results keep getting overwritten. I always only get the result for the last word in the list.</p>
  
  <p>Does anyone know how to modify the code so it will show the result of the whole list of words ?</p>
  
  <p>Here is the code:</p>
  
  <pre><code>from PyQt5 import QtCore, QtGui, QtWidgets
  import random
  import html
  
  words_1 = ["Hello dseerfd", "world sdfsdf sdfgsdf sdfsdf", "Stack dasdf", "Overflow", "Hello world", """&lt;font color="red"&gt;Hello world&lt;/font&gt;"""]
  
  class HTMLDelegate(QtWidgets.QStyledItemDelegate):
      def __init__(self, parent=None):
          super(HTMLDelegate, self).__init__(parent)
          self.doc = QtGui.QTextDocument(self)
  
      def paint(self, painter, option, index):
          substring = index.data(QtCore.Qt.UserRole)
          painter.save()
          options = QtWidgets.QStyleOptionViewItem(option)
          self.initStyleOption(options, index)
          res = ""
          color = QtGui.QColor("red")
          if substring:
              substrings = options.text.split(substring)
              res = """&lt;font color="{}"&gt;{}&lt;/font&gt;""".format(color.name(QtGui.QColor.HexRgb), substring).join(list(map(html.escape, substrings)))
          else:
              res = html.escape(options.text)
          self.doc.setHtml(res)
  
          options.text = ""
          style = QtWidgets.QApplication.style() if options.widget is None \
              else options.widget.style()
          style.drawControl(QtWidgets.QStyle.CE_ItemViewItem, options, painter)
  
          ctx = QtGui.QAbstractTextDocumentLayout.PaintContext()
          if option.state &amp; QtWidgets.QStyle.State_Selected:
              ctx.palette.setColor(QtGui.QPalette.Text, option.palette.color(
                  QtGui.QPalette.Active, QtGui.QPalette.HighlightedText))
          else:
              ctx.palette.setColor(QtGui.QPalette.Text, option.palette.color(
                  QtGui.QPalette.Active, QtGui.QPalette.Text))
  
          textRect = style.subElementRect(
              QtWidgets.QStyle.SE_ItemViewItemText, options)
  
          if index.column() != 0:
              textRect.adjust(5, 0, 0, 0)
  
          the_constant = 4
          margin = (option.rect.height() - options.fontMetrics.height()) // 2
          margin = margin - the_constant
          textRect.setTop(textRect.top() + margin)
  
          painter.translate(textRect.topLeft())
          painter.setClipRect(textRect.translated(-textRect.topLeft()))
          self.doc.documentLayout().draw(painter, ctx)
  
          painter.restore()
  
  
  class Widget(QtWidgets.QWidget):
      def __init__(self, parent=None):
          super(Widget, self).__init__(parent)
          hlay = QtWidgets.QHBoxLayout()
          lay = QtWidgets.QVBoxLayout(self)
          self.table = QtWidgets.QTableWidget(5, 5)
          lay.addLayout(hlay)
          lay.addWidget(self.table)
  
  
  
          self.table.setItemDelegate(HTMLDelegate(self.table))
  
          for i in range(self.table.rowCount()):
              for j in range(self.table.columnCount()):
                  it = QtWidgets.QTableWidgetItem(random.choice(words_1))
                  self.table.setItem(i, j, it)
  
  
          text_list = ['ello', 'ack']
          # clear
          allitems = self.table.findItems("", QtCore.Qt.MatchContains)
  
          selected_items =[]
          for words in text_list:
              for item in allitems:
                  selected_items = self.table.findItems(words, QtCore.Qt.MatchContains)
                  selected_items.append(self.table.findItems(words, QtCore.Qt.MatchContains)) ## i tried to make a list which is beeing appened but using this list it returns only the same as the input
  
                  item.setData(QtCore.Qt.UserRole, words if item in selected_items else None)
  
  
  
  
  if __name__ == '__main__':
      import sys
      app = QtWidgets.QApplication(sys.argv)
      w = Widget()
      w.show()
      sys.exit(app.exec_())
  </code></pre>