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

Phil Thompson phil at riverbankcomputing.com
Thu Oct 9 09:14:07 BST 2008


On Wed, 8 Oct 2008 22:58:05 +0200, "Filip Gruszczyński"
<gruszczy at gmail.com> wrote:
> 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.

Abstract methods only raise an exception if they are actually called. If
your model is empty (eg. if rowCount() returns 0) then this might not
happen.

Phil


More information about the PyQt mailing list