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. Ex:
The QTreeWidget is populated after the data is imported using:
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:
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)
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). 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 (i.e. item_1, item_2), so referring to them is simple. 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
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