How to word wrap a QTreeWidgetItem
Asked Answered
S

2

13

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.

Smashing answered 3/3, 2010 at 9:28 Comment(0)
S
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!!

Smashing answered 3/3, 2010 at 19:59 Comment(5)
Cool solution, thanks for sharing. Also, you might want to change "clinicalBox" to "label" in your answer.Amative
hehe, sorry! That's what happens with naive copy&paste :)Smashing
I'm having a problem with spacing on the QLabel: #21007772Bourassa
You can also implement a custom delegate to do this: #34623536Epicurean
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: #27644804 . I opted for the keep-a-reference version: self.label = QLabel('Long text here'). (I am using PySide version 1.2.0)Garbage
C
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.

Christan answered 3/3, 2010 at 13:31 Comment(4)
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.Smashing
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.Christan
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 jobSmashing
@Smashing it returns the same result prnt.sc/11x0f6f You can see that all other rows uses 2 charecters tooHandbook

© 2022 - 2024 — McMap. All rights reserved.