[PySide] Moving up a tree
Aaron Richiger
a.richi at bluewin.ch
Fri Feb 1 00:00:03 CET 2013
Hello!
As John already mentioned: Some code would have been great and often
says more than many words... One central unclear point is whether you
are using QTreeWidget or QTreeView. I took the 50/50 joker, guessed a
QTreeWidget :-) and wrote a simple example that does what you are
looking for (getting parents and inserting new items around the clicked
item):
################### tree_example.py ########################
#!/usr/bin/python
import sys
from PySide.QtGui import *
class TreeWidgetExample(QTreeWidget):
"""
Tree widget to demostrate getting all parents of a clicked item and to
demonstrate inserting new items around the clicked item.
"""
def __init__(self, parent=None):
super(TreeWidgetExample, self).__init__(parent)
self.setColumnCount(2)
self.setHeaderLabels(["Node", "Value"])
self.build_tree()
def build_tree(self):
"""Builds the tree according to your example on the Mailing
list."""
value = "example value"
# root items
nodeA = QTreeWidgetItem(self, ["A", value])
nodeB = QTreeWidgetItem(self, ["B", value])
# items on first child level
node1 = QTreeWidgetItem(nodeA, ["1", value])
node2 = QTreeWidgetItem(nodeA, ["2", value])
node3 = QTreeWidgetItem(nodeB, ["3", value])
node4 = QTreeWidgetItem(nodeB, ["4", value])
# items on second child level
QTreeWidgetItem(node1, ["a", value])
QTreeWidgetItem(node1, ["b", value])
QTreeWidgetItem(node2, ["c", value])
QTreeWidgetItem(node2, ["d", value])
QTreeWidgetItem(node3, ["e", value])
QTreeWidgetItem(node3, ["f", value])
QTreeWidgetItem(node4, ["g", value])
QTreeWidgetItem(node4, ["h", value])
self.itemClicked.connect(self.onItem)
def onItem(self, item, column_nr):
"""
Slot to be called if an item was clicked. Adds 2 new items to the
tree and prints information about the item and it's parents.
"""
# Comment this out, if you do not like having more and more
items...
self.addItems(item)
print "Node %s clicked in column nr %d" % (item.text(0), column_nr)
print "\tParents: " + str([str("Node %s" % node.text(0)) for
node in self.getParents(item)])
print
def getParents(self, item):
"""
Return a list containing all parent items of this item.
The list is empty, if the item is a root item.
"""
parents = []
current_item = item
current_parent = current_item.parent()
# Walk up the tree and collect all parent items of this item
while not current_parent is None:
parents.append(current_parent)
current_item = current_parent
current_parent = current_item.parent()
return parents
def addItems(self, item):
"""
Inserts 2 items for demonstration purpose:
- 1 on the same level as the clicked item
- 1 as a child of the clicked item
"""
parents = self.getParents(item)
direct_parent = parents[0] if parents else self
QTreeWidgetItem(direct_parent, ["new1", "New node on same level"])
QTreeWidgetItem(item, ["new2", "New node on child level"])
def main():
app = QApplication(sys.argv)
ex = TreeWidgetExample()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
#################### end of code ##############################
I hope it helps!
Aaron
> Ok since last email I don't even understand my question, so I don't
> know how I expect you to.
>
> Let's try this again.
>
> I am using python, pyside, QT. I have an GUI that displays some data
> in a Tree structure, with drop down arrows. The structure resembles a
> file structure with 'files' and 'subfiles'. So in the GUI if I click
> on an item in the tree structure, I can get it to display the name of
> the item selected, but when I try to find it's parent and top parent I
> can't figure it out.
> Same example as before.
> A
> 1
> a
> b
> 2
> B
> 3
> c
> 4
> C
> 5
> 6
> d
> e
>
> So let's say I click on 'e' in the GUI, I can get it to identify that
> I selected 'e', but when I try to get the parents, 6 and C, I get 'd',
> '6', '5', 'C', 'B', 'A'
>
>
> _______________________________________________
> PySide mailing list
> PySide at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/pyside
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20130201/faf0ecb4/attachment.html>
More information about the PySide
mailing list