Hello,

How would one identify the parent of a selected item in a QTreeWidget?

I have a tree with several branches. Each branch contains leaves with the same names. (See attached sample code.) I'd like to be able to identify that the leave item selected, say "Bbbb," was picked from branch "Three."

I'm identifying the selected leaf item with currentItem(). (See line 126 in pickItem()). The currentIndex() method returns indeces that appear to be relative to the leaf's parent, which in this case doesn't help me identify which branch item is the selected item's parent.

Thanks in advance!
Scott




#!/usr/bin/env python



#-------------------------------------------------------------------------------
# imports
#-------------------------------------------------------------------------------
import sys
from PyQt4 import QtCore, QtGui, QtSql



#-------------------------------------------------------------------------------
# class
#-------------------------------------------------------------------------------
class TreeTest(QtGui.QDialog):

	def __init__(self, parent=None):
		QtGui.QDialog.__init__(self)



		# init widgets
		# ------------------------------------------------
		self.theFrame		    = QtGui.QFrame()
		self.theFrame.setFrameStyle(QtGui.QFrame.NoFrame)
		


		# tree
		# ------------------------------------------------
		self.theGroup           = QtGui.QGroupBox()
		self.theTree            = QtGui.QTreeWidget()

		self.theTree.clear()		
		self.theTree.headerItem().setText(0, "The Tree")
		self.theTree.setAlternatingRowColors(True)

		self.theLayout 		    = QtGui.QVBoxLayout()
		self.theLayout.setContentsMargins(0, 0, 0, 0)
		self.theLayout.addWidget(self.theTree)
		self.theGroup.setLayout(self.theLayout)
		
		



		# main layout
		# ------------------------------------------------
		self.formLayout		    = QtGui.QHBoxLayout()
		self.formLayout.setContentsMargins(0, 0, 0, 0)
		self.formLayout.addWidget(self.theGroup)
		self.theFrame.setLayout(self.formLayout)

		self.theLayout		    = QtGui.QVBoxLayout()
		self.theLayout.setContentsMargins(0, 0, 0, 0)
		self.theLayout.addWidget(self.theFrame)
		self.setLayout(self.theLayout)
		




		# signals/slots
		# ------------------------------------------------
		self.connect(self.theTree, QtCore.SIGNAL("itemSelectionChanged()"), self.selectItem)



		# defaults
		# ------------------------------------------------
		tocItemList = self.setTheTree()
		for item in tocItemList:
			print item

		print ""



	# methods
	# ------------------------------------------------

	def setTheTree(self):

		# init toc item list
		tocItemList   = []

		tocDict       = self.setTableOfContents()
		tocKeys       = tocDict.keys()
		tocKeys.sort()

		thePageList   = ["Aaaa", "Bbbb", "Cccc"]

		# set ONE
		oneRootStr    = "One"
		oneRoot       = QtGui.QTreeWidgetItem(self.theTree, [oneRootStr])
		tocItemList.append(oneRootStr)
		self.theTree.expandItem(oneRoot)

		for one in tocDict["one"]:
			oneStr        = str(one)
			one           = QtGui.QTreeWidgetItem(oneRoot, [oneStr])
			tocItemList.append(oneStr)

			for page in thePageList:
				pageStr       = str(page)
				page          = QtGui.QTreeWidgetItem(one, [pageStr])


		return tocItemList


		
	def setTableOfContents(self):
		tocDict       = {}

		# one
		oneList       = ["One", "Two", "Three"]
		tocDict["one"]   = oneList
		
		return tocDict
	


	def selectItem(self):
		print self.theTree.currentItem().text(0)
		index = self.theTree.currentIndex()
		print "row: ", index.row()    # returns row number relative to branch parent
		print ""

		


#-------------------------------------------------------------------------------
# main
#-------------------------------------------------------------------------------
if __name__ == "__main__":

	app = QtGui.QApplication(sys.argv)
	form = TreeTest()
	form.setWindowTitle("Tree Test")
	form.show()
	sys.exit(app.exec_())



_______________________________________________
PyQt mailing list    [email protected]
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to