Aleksey
Aleksey

Reputation: 201

Auto-growing of QTreeWidget(or QTableWidget)

I placed QTreeWidget into QVBoxLayout.

self._tree = QTreeWidget()
layout = QVBoxLayout()
layout.addWidget(self._some_other_widget)
layout.addWidget(self._tree)
layout.addStretch()
self.setLayout(layout)

All nodes in this tree widget are always expanded. I would like to create this tree widget with minimal height (for example, for one row/item). When new items are added this tree widget also grows to show all items. How can this be done? Thank you in advance.

Upvotes: 0

Views: 225

Answers (1)

Aleksey
Aleksey

Reputation: 201

There are at least two solutions:

  1. Use self._tree.setSizeAdjustPolicy(QAbstractScrollArea.AdjustToC‌​ontents). Thank you for this, G.M.

  2. I have also found another solution:

    def sizeHint(self):
        return self._sizeHint()
    
    def minimumSizeHint(self):
        return self._sizeHint()
    
    def _sizeHint(self):
        sh = super().sizeHint()
        if self._item_count > 0:
            return QSize(sh.width(), self.sizeHintForRow(0) * self._item_count)
        else:
            return QSize(sh.width(), 40)
    

Upvotes: 0

Related Questions