亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

增加 qtreewidget 的大小

增加 qtreewidget 的大小

大話西游666 2023-09-26 14:06:55
事實證明,我在 PyQT5 中為 Qtreewidget 發出信號是一個問題。當您單擊“增加”時,您需要增加 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()
查看完整描述

1 回答

?
守候你守候我

TA貢獻1802條經驗 獲得超10個贊

如果您想根據子項是展開還是折疊來更改 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_()


查看完整回答
反對 回復 2023-09-26
  • 1 回答
  • 0 關注
  • 211 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號