我的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());
}
这使滚动条工作,但只有在我展开/折叠树视图项时。
我需要“每次都”使用水平滚动条,从启动应用程序到结束。怎么组织起来?
谢谢。
答案 0 :(得分:11)
This FAQ entry可能会有所帮助。
简而言之:
答案 1 :(得分:3)
如果您使用QT5,请尝试将treewidget设为“水平”自动滚动:
headerStretchLastSection
。
和ui->treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
答案 2 :(得分:2)
对我有用的是:
horizontalScrollBarPolicy
属性设置为ScrollBarAsNeeded
。headerMinimumSectionSize
属性设置为与'几何宽度相同的值'值。headerDefaultSectionSize
属性设置为headerMinimumSectionSize
值的两倍左右。headerStretchLastSection
属性(如其他地方所述)。我是在我修改的表单上使用Qt Designer完成的。
答案 3 :(得分:0)
我刚刚发现另一个案例,其中水平滚动条不会出现在自定义的treeView类中。那是你设置" setHeaderHidden()"真实的不要覆盖resizeEvent()。这正是发生在我身上的事情。我通过调用slot,resizeColumnToContents(0)来覆盖resizeEvent(),因为我在自定义树视图类中只有一列来使水平滚动条工作。
认为这可能对某人有帮助。
答案 4 :(得分:0)
在我看来,截断具有后缀椭圆(即“...”)的树项而不是显示水平滚动条的默认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应该比公然的非智能默认值更好。