QTreeView horizontal scrollbar issues

I have a problem with the QTreeView horizontal scrollbar, it does not appear. I set the horizontal scroll policy in ScrollBarAsNeeded, but it does not appear if necessary. We tried to connect extended and flattened signals to the slot:

connect(this, SIGNAL(expanded(QModelIndex)), this, SLOT(update_scroll_area(QModelIndex))); connect(this, SIGNAL(collapsed(QModelIndex)), this, SLOT(update_scroll_area(QModelIndex))); 

The slot consists of one line of code:

 update_scroll_area(const QModelIndex& i) { resizeColumnToContents(i.column()); } 

This does the work of the scrollbar, but only when I expand / collapse the elements of the tree.

I need to have a horizontal β€œevery time” scrollbar, from application to end. How can this be organized?

Thanks.

+7
source share
5 answers

This FAQ section may help.

In a nutshell:

  • Set a horizontal heading to resize the contents of the column (this applies even if the heading is hidden)
  • Disable the "stretchLastHeaderSection" property to prevent the header from resizing horizontally to the width of the viewport (which seems to override the above setting for resizing the column)
+11
source

What worked for me was:

  • Set the horizontalScrollBarPolicy property to ScrollBarAsNeeded .
  • Set the horizontal header property headerMinimumSectionSize to the same value as the Geometry Width value.
  • Set the horizontal header property headerDefaultSectionSize about twice the value of headerMinimumSectionSize .
  • Disable the horizontal header property headerStretchLastSection (as described elsewhere).

I did this with Qt Designer in a form that I modified.

+2
source

if you are using QT5, try doing this to create a "horizontal" treewidget auto-collection:

  • Disable horizontal header headerStretchLastSection . and
  • ui->treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
+1
source

I just found out another case where the horizontal scrollbar will not be displayed in the custom treeView class. That is, when you set "setHeaderHidden ()" to true and do not override resizeEvent (). This is exactly what happened to me, and I redefined resizeEvent () by calling the slot, resizeColumnToContents (0), since I only have one column in my custom tree class to make the horizontal scrollbar work.

Thought it might be useful to someone.

0
source

In my opinion, the default behavior of QTreeWidget truncating tree elements using a suffix ellipse (ie "...") instead of displaying a horizontal scrollbar is crazy, useless and never what it wants. But this is what we got.

The following subclass of PySide2 QTreeWidget intelligently eliminates this flaw in the column- QTreeWidget value to scale the number of columns in the current tree:

 from PySide2.QtWidgets import QHeaderView, QTreeWidget class QScrollableTreeWidget(QTreeWidget): ''' :mod:`QTreeWidget`-based widget marginally improving upon the stock :mod:`QTreeWidget` functionality. This application-specific widget augments the stock :class:`QTreeWidget` with additional support for horizontal scrollbars, automatically displaying horizontal scrollbars for all columns whose content exceeds that column's width. For unknown reasons, the stock :class:`QTreeWidget` intentionally omits this functionality. ''' def __init__(self, *args, **kwargs) -> None: super().__init__(*args, **kwargs) # Header view for this tree. header_view = self.header() # To display a horizontal scrollbar instead of an ellipse when resizing # a column smaller than its content, resize that column section to its # optimal size. For further details, see the following FAQ entry: # https://wiki.qt.io/Technical_FAQ#How_can_I_ensure_that_a_horizontal_scrollbar_and_not_an_ellipse_shows_up_when_resizing_a_column_smaller_than_its_content_in_a_QTreeView_.3F header_view.setSectionResizeMode(QHeaderView.ResizeToContents) # By default, all trees contain only one column. Under the safe # assumption this tree will continue to contain only one column, prevent # this column content from automatically resizing to the width of the # viewport rather than this column section (as requested by the prior # call). This unfortunate default overrides that request. header_view.setStretchLastSection(False) def setColumnCount(self, column_count: int) -> None: super().setColumnCount(column_count) # If this tree now contains more than one column, permit the last such # column content to automatically resize to the width of the viewport. if column_count != 1: self.header().setStretchLastSection(True) 

Theoretically, this implementation should be trivially rewritable in both PyQt5 and C ++. Because Qt deserves better than frankly stupid defaults.

0
source

All Articles