1

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 ?

JPFrancoia
  • 4,866
  • 10
  • 43
  • 73
  • I'm not familiar with this toolkit so will not presume to answer. But I'd guess that you need to set [wordWrap](http://qt-project.org/doc/qt-4.8/qtableview.html#wordWrap-prop). – Mike Satteson Feb 06 '15 at 14:02

1 Answers1

1

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()
Community
  • 1
  • 1
JPFrancoia
  • 4,866
  • 10
  • 43
  • 73
  • If you don't have special request of using QTextDocument, you can try QScitinlla [http://www.riverbankcomputing.com/software/qscintilla/download], it's a wrapper for general editor, it can also handle the wordwrap, see the API virtual void QsciScintilla::setWrapMode ( WrapMode mode ) [virtual, slot] at http://pyqt.sourceforge.net/Docs/QScintilla2/classQsciScintilla.html#ac04428d2f90c36458d68a673f107e40c. – Cui Heng Feb 08 '15 at 10:11