2011-07-08 131 views
7

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

這使得滾動條的工作,但只有當我展開/摺疊樹視圖項目。

我需要從開始應用程序到結束,每次都有工作水平滾動條。它如何組織?

謝謝。

回答

11

This FAQ entry可能會有所幫助。

簡而言之:

  • 設置水平標頭的大小調整爲列的內容(這適用即使頭部是隱藏的)
  • 禁用「stretchLastHeaderSection」屬性,以防止橫向頭從自動調整大小到視區的寬度(這似乎覆蓋上述設定來調整到柱的大小)
+0

只有在調整「content to content」和「stretchLastHeaderSection」方法之前調用「setModel」時才能使用。 – Prady

2

什麼爲我工作是:

  • horizontalScrollBarPolicy屬性設置爲ScrollBarAsNeeded
  • 將水平標題的headerMinimumSectionSize屬性設置爲與「幾何寬度」值相同的值。
  • 將橫向標頭的headerDefaultSectionSize屬性設置爲headerMinimumSectionSize值的兩倍。
  • 禁用水平頭的headerStretchLastSection屬性(如別處所述)。

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

1

如果使用QT5嘗試本作treewidget 「水平」 自動滾屏:

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

我只是發現了另一種情況下水平滾動條將不會定製的TreeView類露面。那就是當你設置「setHeaderHidden()」爲真時&不要覆蓋resizeEvent()。這正是發生在我身上的事&我通過調用槽,resizeColumnToContents(0)來覆蓋resizeEvent(),因爲我在自定義樹視圖類中只有一列,以使水平滾動條工作。

認爲這可能對某些人有幫助。

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值得比公然不明智的默認值更好。