PyQt - get list of all checked in QTreeWidget - python

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

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.
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.
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.

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())

Related

binary heap as tree structure searching algorithm

so i guess you are all fimilliar with a binary heap data structure if not.. Brilliant. org say
i.e. a binary tree which obeys the property that the root of any tree is greater than or equal to (or smaller than or equal to) all its children (heap property). The primary use of such a data structure is to implement a priority queue.
will one of the properties of a binary heap is that it must be filled from top to bottom (from root) and from right to left
I coded this algorithm to find the next available spot to insert the next number I add (I hard coded the first nodes so I can track more further down the tree
this search method is inspired by BFS(Breadth First Search) algorithm
note that in this code I only care about finding the next empty node without the need to keep the heap property
I tested the code but I don't think I tested it enough so if you spot problems, bugs or suggest any ideas, every comment is welcomed
def insert(self, data):
if self.root.data == None:
self.root.data = data
print('root', self.root.data)
else:
self.search()
def search(self):
print('search..L31')
queue = [self.root]
while queue:
curr = queue.pop(0)
print(curr.data)
if curr.right_child == None:
print('made it')
return
else:
queue.append(curr.left_child)
queue.append(curr.right_child)
h = Min_heap(10)
h.insert(2)
h.root.left_child = Node(3)
h.root.right_child = Node(5)
h.root.left_child.left_child = Node(8)
h.root.left_child.right_child = Node(7)
h.root.right_child.left_child = Node(9)
# The tree I am building...
# __2__
# / \
# 3 5
# / \ / \
# 8 7 9 ⨂
# ↑
# what am
# looking for
h.search()
there is another way to figuring this out which is basically translating the tree into an array/list using special formulas and then we just assume that the next data we want to insert is the last element in the previous array and then work back through the same formulas but I already know that algorithm and I thought why not trying to solve it as a graph soooo...
You should better implement a binary heap as a list (array). But if you want to do it with node objects that have left/right attributes, then the position for the next node can be derived from the size of the tree.
So if you enrich your heap class instances with a size attribute and maintain that attribute to reflect the current number of nodes in the tree, then the following method will tell you where the next insertion point is, in O(logn) time:
Take the binary representation of the current size plus 1. So if the tree currently has 4 nodes, take the binary representation of 5, i.e. 101. Then drop the leftmost (most significant) bit. The bits that then remain are an encoding of the path towards the new spot: 0 means "left", 1 means "right".
Here is an implementation of a method that will return the parent node of where the new insertion spot is, and whether it would become the "left" or the "right" child of it:
def next_spot(self):
if not self.root:
raise ValueError("empty tree")
node = self.root
path = self.size + 1
sides = bin(path)[3:-1] # skip "0b1" and final bit
for side in sides:
if side == "0":
node = node.left
else:
node = node.right
# use final bit for saying "left" or "right"
return node, ("left", "right")[path % 2]
If you want to guarantee balanced, just add to each node how many items are there or below. Maintain that with the heap. And when placing an element, always go to where there are the fewest things.
If you just want a simple way to place, just randomly place it. You don't have to be perfect. You will still on average be O(log(n)) levels, just with a worse constant.
(Of course your constants are better with the array approach, but you say you know that one and are deliberately not implementing it.)

Create an infinite nested list of list

I have a task to make an list of list following specific rules. List must represent a tree with a root and branches from this root with specific color. Each branch should be represented as a list of its child elements (one black branch generates 3 white; 1 white branch generates 2 black). For example: root=['black'], first branches [['white','white','white']], next iteration should be [[[black,black],[black,black],[black,black]]] and so on.
This infinite list should be stored in global variable. Is it possible?
My code, which generates such list do it only for for a predetermined number of steps.
root = ['b']
def change(root):
for index, item in enumerate(root):
if isinstance(item, list):
change(item)
elif item == 'b':
root[index] = ['w','w','w']
elif item == 'w':
root[index] = ['b','b']
return root
for i in range(3):
tree=change(root)
print(tree)
How can I generate infinite list if it is possible?
If I'm understanding your requirements correctly, this creates the entire infinite tree in memory (but requiring only finite memory because it refers to itself):
black = []
white = [black, black]
black.append([white, white, white])
root = black # or white, if you prefer
Note that the 'b' and 'w' leaf nodes never appear in the tree, as you can never actually reach them.
In the comments on your question, I suggested:
#MartijnPieters Perhaps he [OP] means he needs to write a program that yields a potentially-infinite list of lists that follows a certain pattern.
You replied that that's what you want, so I had a go at it - the code I post involves using a function generator, which you can use to generate a potentially never-ending sequence that follows the pattern you specify (if we were to assume infinite memory).
A short while ago I saw an incredible solution posted by someone to generate the sequence of coprime pairs. It was the first time I'd been made aware of the whole mind-blowing concept of "self-recursive generators". You can use them to do some pretty awesome things, like generate the Kolakoski sequence.
In any case, I had a gut feeling that I could use self-recursive generators to solve your problem. The goods news is that my code works. The bad news is that I have no idea why my code works. Here it is:
from enum import Enum
class Colour(Enum):
BLACK = 'black'
WHITE = 'white'
def generate_tree(curr_colour = Colour.BLACK):
yield [curr_colour]
for sub_tree in generate_tree(Colour.WHITE if curr_colour == Colour.BLACK else Colour.BLACK):
if curr_colour == Colour.WHITE:
tree = [sub_tree, sub_tree]
else:
tree = [sub_tree, sub_tree, sub_tree]
yield tree
if __name__ == '__main__':
generator = generate_tree()
for _ in range(3):
print(next(generator))
Output for n=3
[<Colour.BLACK: 'black'>]
[[<Colour.WHITE: 'white'>], [<Colour.WHITE: 'white'>], [<Colour.WHITE: 'white'>]]
[[[<Colour.BLACK: 'black'>], [<Colour.BLACK: 'black'>]], [[<Colour.BLACK: 'black'>], [<Colour.BLACK: 'black'>]], [[<Colour.BLACK: 'black'>], [<Colour.BLACK: 'black'>]]]
You can use the variant below to have a program that keeps generating the tree indefinitely, waiting at each iteration for user input:
if __name__ == '__main__':
generator = generate_tree()
while True:
print(next(generator))
input()

Python: How to speed up creating of objects?

I'm creating objects derived from a rather large txt file. My code is working properly but takes a long time to run. This is because the elements I'm looking for in the first place are not ordered and not (necessarily) unique. For example I am looking for a digit-code that might be used twice in the file but could be in the first and the last row. My idea was to check how often a certain code is used...
counter=collections.Counter([l[3] for l in self.body])
...and then loop through the counter. Advance: if a code is only used once you don't have to iterate over the whole file. However You are stuck with a lot of iterations which makes the process really slow.
So my question really is: how can I improve my code? Another idea of course is to oder the data first. But that could take quite long as well.
The crucial part is this method:
def get_pc(self):
counter=collections.Counter([l[3] for l in self.body])
# This returns something like this {'187':'2', '199':'1',...}
pcode = []
#loop through entries of counter
for k,v in counter.iteritems():
i = 0
#find post code in body
for l in self.body:
if i == v:
break
# find fist appearence of key
if l[3] == k:
#first encounter...
if i == 0:
#...so create object
self.pc = CodeCana(k,l[2])
pcode.append(self.pc)
i += 1
# make attributes
self.pc.attr((l[0],l[1]),l[4])
if v <= 1:
break
return pcode
I hope the code explains the problem sufficiently. If not, let me know and I will expand the provided information.
You are looping over body way too many times. Collapse this into one loop, and track the CodeCana items in a dictionary instead:
def get_pc(self):
pcs = dict()
pcode = []
for l in self.body:
pc = pcs.get(l[3])
if pc is None:
pc = pcs[l[3]] = CodeCana(l[3], l[2])
pcode.append(pc)
pc.attr((l[0],l[1]),l[4])
return pcode
Counting all items first then trying to limit looping over body by that many times while still looping over all the different types of items defeats the purpose somewhat...
You may want to consider giving the various indices in l names. You can use tuple unpacking:
for foo, bar, baz, egg, ham in self.body:
pc = pcs.get(egg)
if pc is None:
pc = pcs[egg] = CodeCana(egg, baz)
pcode.append(pc)
pc.attr((foo, bar), ham)
but building body out of a namedtuple-based class would help in code documentation and debugging even more.

Qt and PyQt tablewidget growing row count

I am learning Python and trying to write a program to help my dad.
I want it to be excel-like, and it looks great for now, but I have no idea how to make the number of rows (not the columns) in a tableWidget to grow while someones crolls down...
Can I do it using QtDesigner or I have to write the code in the .py file?
Any help is appreciated...
Sorry if I am asking silly questions, I an a noob really...
Here is sort of a proof-of-concept example:
class Widget(QtGui.QWidget):
def __init__(self):
super(Widget, self).__init__()
self.resize(600,400)
layout = QtGui.QVBoxLayout(self)
self.table = QtGui.QTableWidget(20,10)
self.vBar = self.table.verticalScrollBar()
self._vBar_lastVal = self.vBar.value()
layout.addWidget(self.table)
self.vBar.valueChanged.connect(self.scrollbarChanged)
def scrollbarChanged(self, val):
bar = self.vBar
minVal, maxVal = bar.minimum(), bar.maximum()
avg = (minVal+maxVal)/2
rowCount = self.table.rowCount()
# scrolling down
if val > self._vBar_lastVal and val >= avg:
self.table.insertRow(rowCount)
# scrolling up
elif val < self._vBar_lastVal:
lastRow = rowCount-1
empty = True
for col in xrange(self.table.columnCount()):
item = self.table.item(lastRow, col)
if item and item.text():
empty=False
break
if empty:
self.table.removeRow(lastRow)
self._vBar_lastVal = val
You have to rely on the vertical scroll bar of the table widget to signal information about it changing value. So we connect its valueChanged(int) signal to our method.
The scrollbarChanged SLOT will receive the value of the scrollbar. What I am doing here is checking the min and max value of the scrollbar at that moment, and seeing if the current position is at least in the middle. We also check if the current scrollbar value is greater than the last time, because we only want to add rows on a down scroll. If these conditions are true, then we insert a new row.
The act of shrinking it back down is a bit more involved because I am sure you will need to check the last row to make sure it is empty and only remove it if so. My version gives you the rough idea but the math logic would probably need more work. But what it is doing is going through every item in the last row to see if its None or an empty value. If the whole row is empty, it removes it. Thus, you do get a reducing effect when scrolling back up.
Hope this gives you a starting point! Feel free to ask about the code if you need more detailed explanation of any parts.

Variable updating one step behind in list searcher in Tkinter

I'm trying to build a search engine that will check a list and then remove all list items that do not meet the search parameters. I know there is several problems with my program such as it will not add things back to the list when you backspace and in my updating for loop I simply tack on a '*' thinking that it will search for strings only beginning with the current parameters, but I will cross those bridges later.
class StudentFinderWindow(Tkinter.Toplevel):
def __init__(self):
Tkinter.Toplevel.__init__(self) # Create Window
searchResultList = ['student1', 'student2', 'student3'] # Test list.
##### window attributes
self.title('Edit Students') #sets window title.
##### Puts stuff into the window.
# text
editStudentInfoLabel = Tkinter.Label(self,text='Select the student from the list below or search for one in the search box provided')
editStudentInfoLabel.grid(row=0, column=0)
# Entry box
self.searchRepositoryEntry = Tkinter.Entry(self)
self.searchRepositoryEntry.grid(row=1, column=0)
# List box
self.searchResults = Tkinter.Listbox(self)
self.searchResults.grid(row=2, column=0)
This fills the Tkinter Listbox with the original list.
# Search results initial updater.
self.getStudentList()
for student in self.studentList:
self.searchResults.insert(Tkinter.END, student)
##### Event handler
Right here I bind to run the list updater after a key is entered into the search box
self.searchRepositoryEntry.bind('<Key>', self.updateSearch)
This is supposed to run every time a key is pressed. It gets the string that is in the Entry then starts a variable count so I know which index the name is at. After that it run a for loop on the current list supposedly checking to see if it fits the requirement of the parameters and any other letter after it. If it does not match it should delete. The problem is the first time I hit a letter the parameters string is just a blank space and then the next letter the string is the first letter and so on. It is always one step behind. And that is the problem
def updateSearch(self, event):
parameters = self.searchRepositoryEntry.get()
int = 0
currentList = self.searchResults.get(0, Tkinter.END)
for i in currentList:
if not i == parameters + '*':
self.searchResults.delete(int)
int += 1
def getStudentList(self):
global fileDirectory # Gets the directory that all the files are in.
fileList = listdir(fileDirectory)
self.studentList = []
for file in fileList:
self.studentList.append(file[:-4])
I believe I have run into this same problem you describe before, when attempting to make an actively searching ctrl-F feature in one of my programs.
What I found to work is not bind on Key but instead KeyRelease. I'm not entirely sure why this works (probably just a quirk with Tkinter). However, it works.
Snippet's:
The binding
# self.FW.En is an entry widget.
self.FW.En.bind('<KeyRelease>', self.find)
Which would run
def find (self, event):
self.Te.tag_remove('found', '1.0', 'end')
pat = self.FW.En.get()
if len(pat) > 1:
index = '1.0'
while True:
index = self.Te.search(pat, index, nocase=1, stopindex='end')
if not index:
break
lastidex = '%s+%dc' % (index, len(pat))
self.Te.tag_add('found', index, lastidex)
index = lastidex
self.Te.tag_config('found', background='#80ff00')

Categories

Resources