在QTreeWidget中从QLabel删除空间 [英] Remove space from QLabel in a QTreeWidget

查看:229
本文介绍了在QTreeWidget中从QLabel删除空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将QLabel小部件添加到我的QTreeWidget中,以解决QTreeWidget中的自动换行问题. (请参阅如何自动包装QTreeWidgetItem ). QLabel小部件似乎在文本周围有间距,由于某种原因,当文本换行时,它们会消失.标签文本为空白时也不会显示.

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.

我尝试在QLabel上设置setContentsMargin(0,0,0,0),但这没有用.我还尝试了setStyleSheet("border: 0px; margin: 0px; padding: 0px;"),它也无济于事.

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.

截屏:

您可以看到,QT是否决定在单词周围放置间距缓冲区取决于描述的长度.仅在启用自动换行时才会发生.进一步播放似乎表明它依赖于描述字符串中的空格.字符串中没有空格会阻止单词周围的额外空格.可能与QLabel的自动换行属性有关.

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)

推荐答案

您看到的是布局逻辑对QLabel的绘制/定位的影响(您可能会在

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).

您可能会做的是:

您可以通过尝试设置setTextFormat()并为所有自定义项明确使用PlainText或RichText来稍微改变行为.但这可能无济于事.

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.

我的建议是将使用过的QItemDelegate或QStyledItemDelegate子类化,并重新实现sizeHint(const QStyleOptionViewItem& option,const QModelIndex& index),以返回所需的尺寸,高度给您的自定义项目.然后使用setItemDelegate()进行查看.

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.

这篇关于在QTreeWidget中从QLabel删除空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆