I've added QLabel widgets to my QTreeWidget to work around the word wrapping issue in QTreeWidget. (see <a href="https://stackoverflow.com/questions/2370014/how-to-word-wrap-a-qtreewidgetitem" rel="nofollow">how to word wrap a QTreeWidgetItem</a>). 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: <img alt="Tree Screenshot" class="b-lazy" data-src="https://i.stack.imgur.com/D42VL.png" data-original="https://i.stack.imgur.com/D42VL.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />
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)Answer1:
What you see is effect of layouting logic for drawing/positioning of QLabel (you may see these routines in <a href="https://qt.gitorious.org/qt/qt/source/f7b3072924fb57b3979ff4d536eb213270be1047:src/gui/widgets/qlabel.cpp#sizeForWidth" rel="nofollow">https://qt.gitorious.org/qt/qt/source/f7b3072924fb57b3979ff4d536eb213270be1047:src/gui/widgets/qlabel.cpp#sizeForWidth</a>, 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.Answer2:
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.Answer3:
I solved the problem by setting the fixed height or maximum height:
label.setMaximumHeight(label.fontMetrics().height() * n);
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.