QTreeView水平滚动条问题

问题描述:

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

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()); 
} 

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

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

谢谢。

This FAQ entry可能会有所帮助。

简而言之:

  • 设置水平标头的大小调整为列的内容(这适用即使头部是隐藏的)
  • 禁用“stretchLastHeaderSection”属性,以防止横向头从自动调整大小到视区的宽度(这似乎覆盖上述设定来调整到柱的大小)
+0

只有在调整“content to content”和“stretchLastHeaderSection”方法之前调用“setModel”时才能使用。 – Prady

什么为我工作是:

  • horizontalScrollBarPolicy属性设置为ScrollBarAsNeeded
  • 将水平标题的headerMinimumSectionSize属性设置为与“几何宽度”值相同的值。
  • 将横向标头的headerDefaultSectionSize属性设置为headerMinimumSectionSize值的两倍。
  • 禁用水平头的headerStretchLastSection属性(如别处所述)。

我在我修改的窗体上使用了Qt Designer。

如果使用QT5尝试本作treewidget “水平” 自动滚屏:

  • 禁用水平头的headerStretchLastSection。 和
  • ui->treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);

我只是发现了另一种情况下水平滚动条将不会定制的TreeView类露面。那就是当你设置“setHeaderHidden()”为真时&不要覆盖resizeEvent()。这正是发生在我身上的事&我通过调用槽,resizeColumnToContents(0)来覆盖resizeEvent(),因为我在自定义树视图类中只有一列,以使水平滚动条工作。

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

在我看来,截断了后面添加椭圆树项目的默认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值得比公然不明智的默认值更好。