QTreeView水平滚动条问题

发布于 2024-11-19 05:59:18 字数 527 浏览 2 评论 0原文

我的 QTreeView 水平滚动条有问题,它没有出现。我已将水平滚动条策略设置为 ScrollBarAsNeeded,但如果需要它不会出现。尝试将展开和折叠的信号连接到插槽:

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

该插槽由一行代码组成:

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

这使得滚动条工作,但仅当我展开/折叠树视图项目时。

我需要“每次”都有工作水平滚动条,从启动应用程序到结束。如何组织?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

分开我的手 2024-11-26 05:59:18

此常见问题解答条目可能会有所帮助。

简而言之:

  • 设置水平标题以调整大小以适应列的内容(即使标题被隐藏,这也适用)
  • 禁用“stretchLastHeaderSection”属性以防止水平标题自动调整为视口的宽度(这似乎是覆盖上面的设置以调整为列的大小)

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)
雪化雨蝶 2024-11-26 05:59:18

如果您使用 QT5,请尝试此操作以使 treewidget “水平”自动滚动:

  • 禁用水平标题的 headerStretchLastSection
  • ui->treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);

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

  • Disable the horizontal header's headerStretchLastSection.
    and
  • ui->treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
烟凡古楼 2024-11-26 05:59:18

对我有用的是:

  • horizo​​ntalScrollBarPolicy 属性设置为 ScrollBarAsNeeded
  • 将水平标题的 headerMinimumSectionSize 属性设置为与“几何宽度”值相同的值。
  • 将水平标题的 headerDefaultSectionSize 属性设置为 headerMinimumSectionSize 值的大约两倍。
  • 禁用水平标题的 headerStretchLastSection 属性(如其他地方所述)。

我在我正在修改的表单上使用 Qt Designer 完成了此操作。

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.

三人与歌 2024-11-26 05:59:18

在我看来,使用后缀椭圆(即“...”)截断树项而不是显示水平滚动条的默认 QTreeWidget 行为是疯狂的、无用的,并且永远不会任何人都想要的。但这就是我们得到的。

以下 PySide2 特定的 QTreeWidget 子类以列感知方式智能地解决了这一缺陷,可缩放到当前树中的列数:

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)

理论上,此实现应该可以轻松地重写为 PyQt5 和 C++。因为 Qt 应该比明显不智能的默认设置更好。

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.

遥远的她 2024-11-26 05:59:18

我刚刚发现另一种情况,水平滚动条不会显示在自定义 treeView 类中。也就是说,当您将“setHeaderHidden()”设置为 true &不要重写 resizeEvent()。这正是发生在我和我身上的事情。我通过调用插槽 resizeColumnToContents(0) 覆盖了 resizeEvent() ,因为我的自定义树视图类中只有一列来使水平滚动条工作。

认为这可能对某人有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文