1

Without custom delegate everything works fine:

enter image description here

But my tableview shows search results and part of the text needs to be bold to indicate where it matches the searched query.

Once I use the delegate to get html tags working, the text that overflows cells is not cliped and replaced with ellipsis:

enter image description here

Heres my delegate:

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 & 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())

also if I would add line

self.doc.setTextWidth(option.rect.width())

it would cut the text to the other line(I increased row height to show it):

enter image description here

1 Answer 1

2
painter.setClipRect(textRect.translated(-textRect.topLeft()))

I thoughtI had it in my delegate, its in all other answers around here, well it does the clipping correctly, though no fancy ellipsis, but thats ok I guess. I assumed previously that theres just some value that I have to enable to get clipping ellipsis, but I am starting to see that it could be considerably more complicated than that, that I might actually need to edit the text itself depending on area rectangle - vs text width taken and adjust on every column resize or some stuff

oh, how I wish they would just make option to enable html rich text with the default delegate

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.