[PyQt] QAbstractItemModel doesn't require to subclass data method

Filip Gruszczyński gruszczy at gmail.com
Wed Oct 8 21:58:05 BST 2008


When I run following code (of course with some implementation of tree objects):

class QCategorizedItemModel(QAbstractItemModel):
		
	def __init__(self, tree):
		QAbstractItemModel.__init__(self)
		self.__root = tree

	def root(self):
		return self.__root
		
	def columnCount(self, parent):
		# there is only one column with all categories inside
		return 1
		
	def rowCount(self, parent):
		return parent.internalPointer().childrenCount()

	def flags(self, index):
		if not index.isValid():
			return 0
		return Qt.ItemIsSelectable
			
	def index(self, row, column, parent):
		if column != 0:
			return QModelIndex()
		child = parent.internalPointer().child(row)
		if child:
			return self.createIndex(row, column, child)
		else:
			return QModelIndex()
			
	def parent(self, index):
		internal = index.internalPointer()
		if internal == self.__pointer:
			return QModelIndex()
		return createIndex(internal.childNumber(), 0, internal)

it displayed an empty tree view and didn't shout about the need of
subclassing data method, which is pure virtual. I don't know it that's
a problem.

-- 
Filip Gruszczyński



More information about the PyQt mailing list