title: PyQt / Qt, tableview with custom delegate to use ellipsis for text overflowing cell
body: <p>Without custom delegate everything works fine:</p>
  
  <p><img src="https://i.imgur.com/hb46SAg.png" alt="enter image description here"></p>
  
  <p>But my tableview shows search results and part of the text needs to be bold to indicate where it matches the searched query.</p>
  
  <p>Once I use the delegate to get html tags working, the text that overflows cells is not cliped and replaced with ellipsis:</p>
  
  <p><img src="https://i.stack.imgur.com/irCoT.png" alt="enter image description here"></p>
  
  <p>Heres my delegate:</p>
  
  <pre><code>class HTMLDelegate(QStyledItemDelegate):
  def __init__(self, parent=None):
      super().__init__()
      self.doc = QTextDocument(self)
  
  def paint(self, painter, option, index):
      painter.save()
  
      options = QStyleOptionViewItem(option)
      self.initStyleOption(options, index)
  
      self.doc.setHtml(options.text)
      options.text = ""
  
      style = QApplication.style() if options.widget is None \
          else options.widget.style()
      style.drawControl(QStyle.CE_ItemViewItem, options, painter)
  
      ctx = QAbstractTextDocumentLayout.PaintContext()
  
      if option.state &amp; QStyle.State_Selected:
          ctx.palette.setColor(QPalette.Text, option.palette.color(
                               QPalette.Active, QPalette.HighlightedText))
  
      textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options)
      #textRect.adjust(0, 0, 0, 0)
      painter.translate(textRect.topLeft())
      self.doc.documentLayout().draw(painter, ctx)
  
      painter.restore()
  
  def sizeHint(self, option, index):
      return QSize(self.doc.idealWidth(), self.doc.size().height())
  </code></pre>
  
  <p>also if I would add line</p>
  
  <pre><code>self.doc.setTextWidth(option.rect.width())
  </code></pre>
  
  <p>it would cut the text to the other line(I increased row height to show it):</p>
  
  <p><img src="https://i.stack.imgur.com/xwFp6.png" alt="enter image description here"></p>