tags:

views:

317

answers:

2

hello all, 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 konw 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 much appreciated. have a good day all.

A: 

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.

jcoon
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
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.
jcoon
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
+4  A: 

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
Cool solution, thanks for sharing. Also, you might want to change "clinicalBox" to "label" in your answer.
Jesse Aldridge
japs