增加 qtreewidget 的大小

Posted

技术标签:

【中文标题】增加 qtreewidget 的大小【英文标题】:Increase the size of qtreewidget 【发布时间】:2021-01-15 13:26:48 【问题描述】:

在 PyQT5 中为 Qtreewidget 发出信号对我来说是个问题。当你点击“增加”时你需要增加 QTreeWidget 的大小,当你再次点击时减小它(就像 Excel 中的过滤器一样)

代码:

 self.tree_countries.setGeometry(QRect(1050, 100, 200, 250))
 self.tree_countries.setHeaderHidden(True)
 self.tree_countries.setStyleSheet("QTreeWidget \n"
                                   "border: 1px solid #3b3838;\n"
                                   "font: 57 12pt \"Google Sans Medium\";\n"
                                   "color: white;\n"
                                   "background-color: #3b3838\n"
                                   "\n")
 sql = filter_sql()
 parent = QTreeWidgetItem(self.tree_countries)
 parent.setText(0, "Страна")
 parent.setFlags(parent.flags() | Qt.ItemIsTristate | Qt.ItemIsUserCheckable)
 for country in sql.all_countries():
     country = str(country)
     child = QTreeWidgetItem(parent)
     child.setFlags(child.flags() | Qt.ItemIsUserCheckable)
     child.setText(0, country[2:-3])
     child.setCheckState(0, Qt.Unchecked)
 self.tree_countries.show()

【问题讨论】:

相关话题***.com/questions/45123466/… 什么是“增加”? 【参考方案1】:

如果你想根据子项是展开还是折叠来改变 QTreeWidget 的高度,那么你必须使用展开和折叠的信号。您还必须计算所需的高度。

from PyQt5 import QtCore, QtWidgets


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.tree = QtWidgets.QTreeWidget()
        self.tree.header().hide()

        root_item = QtWidgets.QTreeWidgetItem(self.tree)
        root_item.setText(0, "root")
        root_item.setFlags(
            root_item.flags() | QtCore.Qt.ItemIsTristate | QtCore.Qt.ItemIsUserCheckable
        )

        for i in range(10):
            child = QtWidgets.QTreeWidgetItem(root_item)
            child.setFlags(child.flags() | QtCore.Qt.ItemIsUserCheckable)
            child.setText(0, "Child-".format(i))
            child.setCheckState(0, QtCore.Qt.Unchecked)

        central_widget = QtWidgets.QWidget()
        self.setCentralWidget(central_widget)
        lay = QtWidgets.QVBoxLayout(central_widget)
        lay.addWidget(self.tree)
        lay.addStretch(1)

        self.tree.collapsed.connect(self.handle_collapsed)
        self.tree.expanded.connect(self.handle_expanded)

    def handle_collapsed(self, index):
        h = self.tree.rowHeight(index) + self.tree.header().height() + 1
        self.adjust_height(h)

    def handle_expanded(self):
        h = self.tree.sizeHint().height()
        self.adjust_height(h)

    def adjust_height(self, height):
        self.tree.setFixedHeight(height)

    def showEvent(self, event):
        index = self.tree.indexFromItem(self.tree.topLevelItem(0))
        if self.tree.isExpanded(index):
            QtCore.QTimer.singleShot(0, self.handle_expanded)
        else:
            QtCore.QTimer.singleShot(
                0, lambda index=index: self.handle_collapsed(index)
            )
        super().showEvent(event)


app = QtWidgets.QApplication([])

w = MainWindow()
w.show()

app.exec_()

【讨论】:

以上是关于增加 qtreewidget 的大小的主要内容,如果未能解决你的问题,请参考以下文章

使用自定义数据将 QTreeWidgetItem 拖放到 QGraphicsView

调整大小时如何使 QTreeWidget 调用 sizeHint?

如何对 QTreeWidget 列进行排序?

如何在 QTreeWidget 中设置小列宽?

如何在 QTreeWidget/QTreeView 中获取行高

包含小部件和设置行高问题的 QTreeWidget