How to word wrap a QTreeWidgetItem

无人久伴 提交于 2020-01-11 00:45:28

问题


I have to populate a QTreeWidget with items (or children of items) that may happen to be too long to fit in a single line, so I'm looking for a way to word wrap them.

I thought

myQTreeWidget.setWordWrap(True)

(done via QtDesigner4) would have done the job, but that doesn't seem to be the case.

I don't know if it is relevant, but the tree is enveloped in a splitter frame, so the wrapping should be somehow dynamic to allow resizing of the splitter.

Any ideas? I use PyQt4, but hints in any language/binding would be appreciated.


回答1:


I successfully found a workaround: i envelope a QLabel in the WidgetItem, setting the QLabel to have word wrap capabilities.

item = QTreeWidgetItem()
label = QLabel('long text here')
label.setWordWrap(True)

MyTree.addTopLevelItem(item)
MyTree.setItemWidget(item, 0, label)

the behaviour is exactly the one desired!!




回答2:


setWordWrap just causing wrapping around word-boundaries... it will NOT force anything on to a new line.

What you are looking for is not possible with the default QTreeWidget. I suggest displaying text that is too long in an alternative way, such as mouse-over text or a separate label. TreeViews should not contain more then one line of text per item.



来源:https://stackoverflow.com/questions/2370014/how-to-word-wrap-a-qtreewidgetitem

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!