Implementing a delegate for wordwrap in a QTreeView (Qt/PySide/PyQt)?

試著忘記壹切 提交于 2019-12-01 00:46:22

The issue seems to stem from the fact that the value for option.rect.width() passed into QStyledItemDelegate.sizeHint() is -1. This is obviously bogus!

I've solved this by storing the width in the model from within the paint() method and accessing this from sizeHint().

So in your paint() method add the line:

index.model().setData(index, option.rect.width(), QtCore.Qt.UserRole+1)

and in your sizeHint() method, replace document.setTextWidth(option.rect.width()) with:

width = index.model().data(index, QtCore.Qt.UserRole+1)
if not width:
    width = 20
document.setTextWidth(width)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!