12

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.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
japs
  • 1,286
  • 13
  • 27

2 Answers2

21

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!!

japs
  • 1,286
  • 13
  • 27
  • Cool solution, thanks for sharing. Also, you might want to change "clinicalBox" to "label" in your answer. – Jesse Aldridge Mar 03 '10 at 20:51
  • 1
    hehe, sorry! That's what happens with naive copy&paste :) – japs Mar 24 '10 at 12:19
  • I'm having a problem with spacing on the QLabel: http://stackoverflow.com/questions/21007272/remove-space-from-qlabel-in-a-qtreewidget – Jason Jan 08 '14 at 21:49
  • You can also implement a custom delegate to do this: http://stackoverflow.com/questions/34623036/implementing-a-delegate-for-wordwrap-in-a-qtreeview-qt-pyside-pyqt – eric Jan 06 '16 at 03:53
  • Note that this worked for me, BUT I was getting a crash. According to this question, you need to either set a label for the parent or keep a reference to it: http://stackoverflow.com/questions/27644304/setitemwidget-causing-crash . I opted for the keep-a-reference version: `self.label = QLabel('Long text here')`. (I am using PySide version 1.2.0) – Becca codes Mar 10 '17 at 20:18
0

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.

Jason Coon
  • 17,601
  • 10
  • 42
  • 50
  • 1
    but if i do item = QTreeWidgetItem(['some text \n and some more']) it is displayed exactly as one would expect: an item on two lines. – japs Mar 03 '10 at 13:53
  • 1
    Real (dynamic) word-wrapping is not possible though. The only thing you might do to simulate word-wrap is write your own word-wrap function that adds newlines to the item stings based on the width of the widget, and then create a signal to call that function when the size of the widget changes. – Jason Coon Mar 03 '10 at 13:58
  • 1
    I'm trying to envelope a QLabel or a QText-something within an item, so that then i inherit the word wrapping capabilities form the text box, whatever the one it happens to do the right job – japs Mar 03 '10 at 19:04
  • @japs it returns the same result https://prnt.sc/11x0f6f You can see that all other rows uses 2 charecters too –  Apr 23 '21 at 09:19