【问题标题】:List all QtreeWidget hierarchy列出所有 QtreeWidget 层次结构
【发布时间】:2018-12-06 23:38:53
【问题描述】:

我有一个列出的 QTreeWidget

  column 0   |  column 1   |column 2
-----------------------------------

    file1
      +      | found item  | (checkbox) new item
      +      | found item  | (checkbox) new item 
    file2
      +      | found item  | (checkbox) new item
             | found item  | (checkbox) new item 

我正在寻找一种在用户点击按钮时显示所有数据的方法,如果未选中新项目,它将跳过该行。

我发现了与here 类似的东西,但这只会打印已选中/未选中的项目。我该如何为所有人做到这一点?

【问题讨论】:

  • 我不明白你想要的输出是什么,例如你展示的例子的输出是什么
  • 公平点。本质上,我想最终创建一个字典,其中文件是键,它包含一个元组列表,例如:{fileX: [(found_item, new_item),(found_item,new_item)], file2:[(found_item, new_item)......]} 但首先,我只想找到所有内容的值,所以我可以创建字典
  • 该元组应该只包含选中的项目吗?
  • 如果新项目被选中,元组应该只包含found_item和new_item..
  • 一些反馈??

标签: python pyqt pyside qtreewidget


【解决方案1】:

想法是遍历item并验证条件:

from PyQt4 import QtCore, QtGui
import random

class Widget(QtGui.QWidget):
    def __init__(self):
        super(Widget, self).__init__()
        self.tree = QtGui.QTreeWidget(columnCount=3)
        self.tree.setHeaderLabels(["column {}".format(i) for i in range(3)])
        button = QtGui.QPushButton("Press Me", 
            clicked=self.on_clicked)
        lay = QtGui.QVBoxLayout(self)
        lay.addWidget(self.tree)
        lay.addWidget(button)
        self.create_data()
        self.resize(640, 480)

    def create_data(self):
        for i in range(4):
            it = QtGui.QTreeWidgetItem(["file{}".format(i)])
            self.tree.addTopLevelItem(it)
            for j in range(random.randint(2, 5)):
                vals = ["+", "found item-{}-{}".format(i, j), "new item-{}-{}".format(i, j)]
                child_it = QtGui.QTreeWidgetItem(vals)
                child_it.setCheckState(2, random.choice([QtCore.Qt.Checked, QtCore.Qt.Unchecked]))
                it.addChild(child_it)
        self.tree.expandAll()

    @QtCore.pyqtSlot()
    def on_clicked(self):
        d = dict()
        for i in range(self.tree.topLevelItemCount()):
            top_item = self.tree.topLevelItem(i)
            v = []
            for j in range(top_item.childCount()):
                child_item = top_item.child(j)
                if child_item.checkState(2) == QtCore.Qt.Checked:
                    v.append(tuple(child_item.text(i) for i in (1, 2)))
            d[top_item.text(0)] = v
        print(d)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

输出:

{'file0': [('found item-0-2', 'new item-0-2'), ('found item-0-3', 'new item-0-3')], 'file1': [('found item-1-0', 'new item-1-0'), ('found item-1-1', 'new item-1-1'), ('found item-1-2', 'new item-1-2')], 'file2': [('found item-2-0', 'new item-2-0'), ('found item-2-1', 'new item-2-1'), ('found item-2-3', 'new item-2-3')], 'file3': [('found item-3-0', 'new item-3-0'), ('found item-3-1', 'new item-3-1')]}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 2010-11-26
    • 2014-07-20
    • 2019-09-02
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    相关资源
    最近更新 更多