Remove space from QLabel in a QTreeWidget
Asked Answered
G

3

3

I've added QLabel widgets to my QTreeWidget to work around the word wrapping issue in QTreeWidget. (see how to word wrap a QTreeWidgetItem). The QLabel widgets appear to have spacing around the text which for some reason disappears when the text wraps. It also does not show up when the Label text is blank.

I tried setting setContentsMargin(0,0,0,0) on the QLabel but that didn't work. I also tried setStyleSheet("border: 0px; margin: 0px; padding: 0px;") which also didn't help.

Screenshot: Tree Screenshot

You can see that it depends on the length of the description whether QT decides to put that spacing buffer around the words. It only happens when the word wrap is enabled. Further playing around seems to indicate its dependent on spaces in the description string. No spaces in the string prevents the additional space around the words. Probably something to do with what the QLabel is doing with its word wrap property.

# This code is Ruby because I'm using the qtbindings gem
tree = Qt::TreeWidget.new
tree.setColumnCount(2)
tree.setHeaderLabels(["Name", "Description"])

top_node = Qt::TreeWidgetItem.new(["top"])
top_node.setCheckState(0, Qt::Unchecked)
tree.addTopLevelItem(top_node)
desc_label = Qt::Label.new("description")
desc_label.setWordWrap(true) # Remove and it works
tree.setItemWidget(top_node, 1, desc_label)

node = Qt::TreeWidgetItem.new(["test1"])
node.setCheckState(0, Qt::Unchecked)
top_node.addChild(node)
desc_label = Qt::Label.new("description1 is long and very interesting")
desc_label.setWordWrap(true) # Remove and it works
tree.setItemWidget(node, 1, desc_label)
Girl answered 8/1, 2014 at 21:48 Comment(5)
Can you post a screenshot of the problem? I have just tried it and I don't see any additional margins around texts.Miscreant
In addition, provide some code, too that reproduces the issue.Koroseal
I've added a screenshot, code, and a better explanation of what is happening and why.Girl
Don't use widgets, use Qt MVC. This issue may be solved in 3 lines of code with custom delegate.Fortuitous
Care to elaborate Dmitry?Girl
B
1

What you see is effect of layouting logic for drawing/positioning of QLabel (you may see these routines in https://qt.gitorious.org/qt/qt/source/f7b3072924fb57b3979ff4d536eb213270be1047:src/gui/widgets/qlabel.cpp#sizeForWidth, see sizeForWidth() method).

What you may do is:

You may change the behavior little by trying to set setTextFormat() and use PlainText or RichText for all custom items explicitly. But it may not help.

My recommendation is to subclass used QItemDelegate or QStyledItemDelegate and reimplement the sizeHint( const QStyleOptionViewItem & option, const QModelIndex & index ) for returning desired size, height for you customized item. Then to use setItemDelegate() to view.

Berkie answered 25/1, 2014 at 10:29 Comment(2)
I'm not sure how to do this with the flexibility required such as when the user resizes the parent widget which holds the tree. In any case you're probably right about implementing the delegate so you get the bounty.Girl
Would have been great to see it actually implemented for the 100!Fluorene
G
0

My workaround was to set the label's minimum height as follows:

desc_label.setMinimumHeight(desc_label.fontMetrics.height * 2)

This matches what the Label is doing automatically with some of the strings and prevents the inconsistenly sized labels with blank or one-word strings.

Girl answered 27/1, 2014 at 19:51 Comment(0)
L
0

I solved the problem by setting the fixed height or maximum height:

label.setMaximumHeight(label.fontMetrics().height() * n); 

or

label.setFixedHeight(label.fontMetrics().height() * n);

where n is max considered/estimated lines of the label content.

Unfortunately, setting the minimum height, label.setMinimumHeight(...) does not work, otherwise it was more rational since it is not clear that the wrapped text may have how much lines. Also label.setContentMargin(0,0,0,0) does not work.

Limpopo answered 20/2, 2018 at 12:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.