[Python] QStyledItemDelegate on QTreeWidget crashes my application

Discussão em 'Python' iniciado por Stack, Maio 6, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying have some words in my QTreeWidget items to be bold. After some research, I saw a solution using a QStyledItemDelegate to use html formating on my tree items--It works but it is super unstable and keeps crashing my application.

    It doesn't give me an error message except for : process is terminated with return code 3221225477.

    Here's the delegate I'm testing atm :

    class HtmlDelegate(QStyledItemDelegate):

    def paint(self, painter, option, index):
    # Skipping to retain category headers
    if not index.parent().isValid():
    super().paint(painter, option, index)
    return

    painter.save()

    text = index.data()

    doc = QTextDocument()
    doc.setHtml(f"<p style='color: rgb(125, 251, 182);'>{text}</p>")
    doc.setTextWidth(option.rect.width())

    painter.translate(option.rect.topLeft())
    doc.drawContents(painter)

    painter.restore()

    def sizeHint(self, option, index):

    text = index.data()

    doc = QTextDocument()
    doc.setHtml(f"<p>{text}</p>")
    doc.setTextWidth(option.rect.width())

    return doc.size().toSize()


    Thank you!

    Continue reading...

Compartilhe esta Página