8

I've a problem with QTreeView horizontal scrollbar, it doesn't appear. I've set horizontal scrollbar policy to ScrollBarAsNeeded, but it doesn't appear if needed. Have tried to connect expanded and collapsed signals to a 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 makes scrollbar working, but only when I'm expanding/collapsing the tree view items.

I need to have working horizontal scrollbar "every time", from starting the application till its end. How can it be organized?

Thank you.

5 Answers 5

14

This FAQ entry may help.

In a nutshell:

  • Set the horizontal header to resize to the contents of the column (this applies even if the header is hidden)
  • Disable the 'stretchLastHeaderSection' property to prevent the horizontal header from automatically resizing to the width of the viewport (which appears to override the above setting to resize to the size of the column)
2
  • This will only work if "setModel" called before "resize to content" and "stretchLastHeaderSection" methods.
    – Prady
    Jul 21, 2013 at 15:49
  • this will cause the column to shrink and not fill the space so you just need to Set the horizontal header's headerMinimumSectionSize property to the same value as the 'geometry Width' value according to stackoverflow.com/a/26267832/5549635
    – l3enQ
    Jul 27, 2022 at 12:25
8

if you use QT5 try this to make treewidget "horizontal" autoscroll:

  • Disable the horizontal header's headerStretchLastSection. and
  • ui->treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
1
  • this will cause the column to shrink and not fill the space so you just need to Set the horizontal header's headerMinimumSectionSize property to the same value as the 'geometry Width' value according to stackoverflow.com/a/26267832/5549635
    – l3enQ
    Jul 27, 2022 at 12:26
4

What worked for me was to:

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

I did this using Qt Designer on the form I was modifying.

2

In my view, the default QTreeWidget behaviour of truncating tree items with a suffixing ellipse (i.e., "...") rather than displaying a horizontal scrollbar is insane, useless, and never what anyone wants. But it's what we got.

The following PySide2-specific QTreeWidget subclass intelligently addresses this deficiency in a column-aware manner scaling to 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's 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's content from automatically resizing to the width of the
        # viewport rather than this column's 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's content to automatically resize to the width of the viewport.
        if column_count != 1:
            self.header().setStretchLastSection(True)

In theory, this implementation should be trivially rewritable into both PyQt5 and C++. Because Qt deserves better than blatantly unintelligent defaults.

0

I just found out another case where horizontal scroll bar won't show up in custom treeView class. That is when you set "setHeaderHidden()" to true & don't override resizeEvent(). This is exactly what happend to me & i overrode resizeEvent() by calling the slot, resizeColumnToContents(0) as i only have one column in my custom tree view class to make the horizontal scroll bar work.

Thought this might be helpful to some one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.