15432

Remove space from QLabel in a QTreeWidget

Question:

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

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.

Recommend

  • python/pyside using a custom widget in a qtreewidget
  • Read article content using goose retrieving nothing
  • Optimizing GridLayout's memory usage
  • Printing the Table from QTableWidget
  • Getting ParseException on Transport.send(message)
  • php mPDF, impossible to set font-family and font-size
  • Newbie Dojo - Google CDN Question
  • Qt 5.3 QTreeView clickable header data
  • ActionError not getting displayed
  • Binding function to treeview does not work Tkinter
  • Access-Control-Allow-Origin issues even on setting it up
  • Adding image to email via Java mail
  • Changing row height for different treeviews tkitner
  • How to call Rest API DELETE operation with BODY content from Apache Camel?
  • File Streamed well without buffering but gets streamed partially while buffering
  • Jmeter 3.0: JSR223+Groovy vs. BeanShell
  • Download file on ajax success node.js
  • Invoking REST API for making new component in JIRA
  • Qt 4.6 - C++ - QTreeWidgetItem Iterator
  • Apache and Nodejs cross domain ajax issue
  • response.setContentType(“APPLICATION/OCTET-STREAM”)
  • Java cannot send email with html with images
  • Google Cloud Print User credentials required
  • Grails failed to export Excel
  • Writing a proxy in grails
  • File download through ajax
  • Pass json data using Gson and display RecyclerView but still display only last record in android
  • how to check how much of a video stream has been seen by user with php
  • Slow cors preflight OPTIONS Request in Chrome [closed]
  • creating a new tree from the existing trees as left and right
  • Cross compiling Qt 5
  • PyQt4 application on Windows is crashing on exit
  • Google analytics measurement protocol session timeout and query time limits
  • Configure nginx to return different files to different authenticated users with the same URI
  • How to estimate the Kalman Filter with 'KFAS' R package, with an AR(1) transition equation
  • Copy to all folders batch file?
  • How can the INSERT … ON CONFLICT (id) DO UPDATE… syntax be used with a sequence ID?
  • QLPreviewController hide print button in ios6
  • D3 nodes and links from JSON with nested arrays of children
  • Uncaught Error: Could not find module `ember-load-initializers`