Search code examples
pythonpyqtqstyleditemdelegate

Draw adjustable text in QTableView cell


I need to subclass the QStyledItemDelegate of my QTableView. More particularly, I need to modify the display of a specific column. The cells in this column normally contains text. Here is a little part of my custom QStyledItemDelegate class:

elif index.column() == 3:
    title = index.data()
    painter.drawText(option.rect, QtCore.Qt.AlignCenter, title)

But I have a little problem, when I try to display it like this.

Expected:

Expected

Reality:

reality

To get the expected picture, I just have to do nothing on this column in the StyledItemDelegate. I need to do the same as that, but with the function drawText.

Do you have any idea ?


Solution

  • Ok, I found kind of an answer here: Word Wrap with HTML? QTabelView and Delegates

    It also transforms the text to html and allows html formatting (I needed it too), but I think it can easily be transformed to display simple text, with word wrap.

    This snippet is basically for those who want to modify the content and/or the formatting of the content on the fly, through QStyledItemDelegate:

    options = QtGui.QStyleOptionViewItemV4(option)
    self.initStyleOption(options, index)
    
    painter.save()
    
    doc = QtGui.QTextDocument()
    text_option = QtGui.QTextOption(doc.defaultTextOption())
    text_option.setWrapMode(QtGui.QTextOption.WordWrap)
    doc.setDefaultTextOption(text_option)
    
    # Modify the text here. Ex:
    # options.text += "<br><br>"
    doc.setHtml(options.text)
    doc.setTextWidth(options.rect.width())
    
    options.text = ""
    options.widget.style().drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter)
    
    # Center the text vertically
    height = int(doc.documentLayout().documentSize().height())
    painter.translate(options.rect.left(), options.rect.top() + options.rect.height() / 2 - height / 2)
    
    clip = QtCore.QRectF(0, 0, options.rect.width(), options.rect.height())
     doc.drawContents(painter, clip)
    
    painter.restore()