简体   繁体   English

PyQt - 获取所有在 QTreeWidget 中检查的列表

[英]PyQt - get list of all checked in QTreeWidget

I am building a simple application to open up a folder of data and plot that data.我正在构建一个简单的应用程序来打开一个数据文件夹并绘制该数据。 Importing the data updates a QTreeWidget that shows which signals are available to plot.导入数据更新一个 QTreeWidget,显示哪些信号可用于绘制。 Ex:前任:

在此处输入图片说明

The QTreeWidget is populated after the data is imported using:使用以下方法导入数据后填充 QTreeWidget:

def update_treeWidget(self):
    headers = self.df['voltage recording'].columns[1:]
    sweeps = self.df['voltage recording'].index.levels[0]

    for header in headers:
        parent_item = QtWidgets.QTreeWidgetItem(self.treeWidget)
        parent_item.setCheckState(0, QtCore.Qt.Unchecked)
        parent_item.setText(0, header)

        for sweep in sweeps:
            child_item = QtWidgets.QTreeWidgetItem(parent_item)
            child_item.setCheckState(0, QtCore.Qt.Unchecked)
            child_item.setText(0, sweep)

It is unclear to me, though, how I would check the checked status for any of the items now in the tree.但是,我不清楚我将如何检查树中任何项目的已检查状态。

So my two questions are:所以我的两个问题是:

  1. How would I refer to any part of that tree to check it's checked status (either the parent, or the various children of a parent)我将如何引用该树的任何部分来检查它的检查状态(父级或父级的各个子级)

  2. Is there a way to simply return a list of all of the boxes that are checked?有没有办法简单地返回所有选中的框的列表? If I can figure out the answer to Question 1 I can obviously create a function where, every time a box is checked it is added to a list (and removed from that list if it is unchecked).如果我能找出问题 1 的答案,我显然可以创建一个函数,每次选中一个框时,它都会添加到列表中(如果未选中,则从该列表中删除)。 But since the goal here is simply to plot all of the checked signals, the most straightforward thing to me (logically anyway) is upon hitting the "Plot" button the interface first checks to see which signals boxes are checked and then plots those signals.但由于这里的目标只是绘制所有选中的信号,对我来说最直接的事情(无论如何在逻辑上)是点击“绘制”按钮后,界面首先检查哪些信号框被选中,然后绘制这些信号。

In all of the examples I have seem the tree items are explicitly declared (ie item_1, item_2), so referring to them is simple.在所有示例中,我似乎都明确声明了树项(即 item_1、item_2),因此引用它们很简单。 Because of how I am populating the tree, though, I don't understand how to do that.但是,由于我如何填充树,我不明白如何做到这一点。

If this is an incorrect approach to populating the tree in the first place, please let me know and/or point me in the direction of a more correct approach.如果这是首先填充树的不正确方法,请告诉我和/或指出更正确方法的方向。

Thanks谢谢

Edit:编辑:

This is very similar to: PyQT QTreeWidget iterating这非常类似于: PyQT QTreeWidget iterating

which is what I based my answer off of.这就是我的答案所依据的。

Figured it out.弄清楚了。 This at least achieves what I need it to achieve:这至少实现了我需要它实现的目标:

def find_checked(self):
    checked = dict()
    root = self.treeWidget.invisibleRootItem()
    signal_count = root.childCount()

    for i in range(signal_count):
        signal = root.child(i)
        checked_sweeps = list()
        num_children = signal.childCount()

        for n in range(num_children):
            child = signal.child(n)

            if child.checkState(0) == QtCore.Qt.Checked:
                checked_sweeps.append(child.text(0))

        checked[signal.text(0)] = checked_sweeps

    return checked

Next solution can be used:可以使用下一个解决方案:

for item in self.QTreeWidget.findItems("", Qt.MatchContains | Qt.MatchRecursive):
    if (item.rowCount() == 0 and item.checkState()>0):
        print (item.text(),item.checkState())

Five years later I was searching for an answer to your second question.五年后,我一直在寻找你的第二个问题的答案。 While dan_g's code was very helpful I believe it will not list all items if the tree depth is greater than 2. I created a recursive version that will list all selected items regardless of their level in the tree.虽然 dan_g 的代码非常有用,但我相信如果树深度大于 2,它不会列出所有项目。我创建了一个递归版本,它将列出所有选定项目,而不管它们在树中的级别。

    def get_selected_items(self):
        checked_items = []
        def recurse(parent_item):
            for i in range(parent_item.childCount()):
                child = parent_item.child(i)
                grand_children = child.childCount()
                if grand_children > 0:
                    recurse(child)
                if child.checkState(0) == Qt.Checked:
                    checked_items.append(child.text(0))

        recurse(self.ui.treeWidget.invisibleRootItem())
        return checked_items

Or in my case if you only want to list selected items at the leaves of the tree just add an else statement.或者在我的情况下,如果您只想在树的叶子上列出选定的项目,只需添加一个else语句。

    def get_selected_leaves(self):
        checked_items = []
        def recurse(parent_item):
            for i in range(parent_item.childCount()):
                child = parent_item.child(i)
                grand_children = child.childCount()
                if grand_children > 0:
                    recurse(child)
                else: 
                    if child.checkState(0) == Qt.Checked:
                        checked_items.append(child.text(0))

        recurse(self.ui.treeWidget.invisibleRootItem())
        return checked_items

In either case the function calls itself whenever it finds an item that has children until it traverses the entire tree.在任何一种情况下,该函数在找到具有子项的项时都会调用自身,直到它遍历整个树。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM