0
votes

I am using PyQt5 for my application and a QTreeWidget to display content. Since I need to display rich text (HTML) each item has its text property set to "" and I create individual QLabels with the desired text. I then use QTreeWidget.setItemWidget. My problem is that using that method, when the QTreeWidget is smaller (in width) than the items' width, the horizontal scrollbar is not displayed. Which is logical, since from the point of view of the QTreeWidget, each item has width 0 because its text is empty.

I tried using a custom helper method that I use instead of the QLabel.setText method by automatically calling QLabel.setFixedSize method afterwards, but it doesn't work very well (the size is off by 5 to 90 pixels each time).

How would it be possible to make the whole thing determine automatically when to show the scrollbar, and what width to use for them?

MCVE:

tree = QTreeWidget()
item = QTreeWidgetItem(tree)
label = QLabel()
label.setText("<b>some text here</b>")
tree.setItemWidget(item, 0, label)
1
An MCVE is not a list of requirements! You are being asked to show your code.ekhumoro

1 Answers

1
votes

I even struggled with the same problem and finally this solution works for me:

  • Set the treewidget header StretchLastSection to False
  • treeWidget.header()->setSectionResizeMode(column, QtWidgets.QHeaderView.ResizeToContents)