Hello,

What's the proper way to use QTreeWidget's itemAt() method? I'm passing incrementing int values to it, yet it only returns the tree's root item. eg:

                for ii in range(self.treeRowCount):
                        item      = self.theTree.itemAt(0, ii)

yields:

ii:       0
item:     <PyQt4.QtGui.QTreeWidgetItem object at 0x59ed0>
text:     rootItem
ii:       1
item:     <PyQt4.QtGui.QTreeWidgetItem object at 0x59ed0>
text:     rootItem
ii:       2
item:     <PyQt4.QtGui.QTreeWidgetItem object at 0x59ed0>
text:     rootItem
ii:       3
item:     <PyQt4.QtGui.QTreeWidgetItem object at 0x59ed0>
text:     rootItem


(See attached for the full, simple code example.)

Ultimately, I want to be able to identify a QTreeWidgetItem by the results of its text() so that I can set the tree item's parent for branch points. Note that I'm building simple, small trees from stored data sets. Hence the convenience widget rather than the model-view.

Thanks in advance!
Scott




#!/usr/bin/env python

import sys
from PyQt4 import QtCore, QtGui


class TreeWidgetTest(QtGui.QDialog):

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



		# init
		# ------------------------------------------------
		self.theFrame		    = QtGui.QFrame()
		self.treeRowCount       = 0



		# widgets and layouts
		# ------------------------------------------------
		self.theTree            = QtGui.QTreeWidget()

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

		self.treeLayout	        = QtGui.QVBoxLayout()
		self.treeLayout.addWidget(self.theTree)
		self.theFrame.setLayout(self.treeLayout)

		self.theLayout		    = QtGui.QVBoxLayout()
		self.theLayout.addWidget(self.theFrame)
		self.setLayout(self.theLayout)

		# populate tree
		self.populateTree()

		# print tree item text
		self.treeItemText()



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

	def populateTree(self):
		self.theTree.clear()
		self.theTree.setColumnCount(12)

		# set root item
		rootItem   = QtGui.QTreeWidgetItem(self.theTree, ["rootItem"])
		self.treeRowCount += 1

		# add child items
		childItem1 = QtGui.QTreeWidgetItem(rootItem, ["childItem1"])
		self.treeRowCount += 1
		childItem2 = QtGui.QTreeWidgetItem(rootItem, ["childItem2"])
		self.treeRowCount += 1
		childItem3 = QtGui.QTreeWidgetItem(rootItem, ["childItem3"])
		self.treeRowCount += 1

		

	def treeItemText(self):
		for ii in range(self.treeRowCount):
			item      = self.theTree.itemAt(0, ii)
			print "ii:      ", ii
			print "item:    ", item
			print "text:    ", item.text(0)



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

	app = QtGui.QApplication(sys.argv)
	form = TreeWidgetTest()
	form.setWindowTitle("TreeWidgetTest")
	form.show()
	sys.exit(app.exec_())





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

Reply via email to