PDA

View Full Version : Is third input to createIndex really a pointer?



neuronet
15th July 2014, 16:25
I am going through various PySide tutorials, now learning tree views, and trying to understand a couple of things. In the Model class, there is a parent method which (if I am not mistaken) takes in the model, and an index, and returns the index of its parent node. The parent() method calls the built-in method createIndex(), which at the documentation (http://qt-project.org/doc/qt-4.8/qabstractitemmodel.html#createIndex) usage is described thusly:


def createIndex(row, column, ptr): Creates a model index for the given row and column with the internal pointer ptr

While that is from Qt, the description is the same at PySide documentation (http://srinikom.github.io/pyside-docs/PySide/QtCore/QAbstractItemModel.html#PySide.QtCore.PySide.QtCor e.QAbstractItemModel.createIndex) site too.

And in the treemodel tutorial example (http://qt-project.org/doc/qt-4.8/itemviews-simpletreemodel.html), within the parent() method, it is used as follows (trimming away some inessential details):


def parent(self, childIndex):
childNode = childIndex.internalPointer() #internal pointer returns node corresponding to index
parentNode = childNode.parent() #this instance of parent() is defined in the TreeItem class
return self.createIndex(parentNode.row(), 0, parentNode) #second input is 0 by convention for tree parent

My question is this: while we indeed use the internal pointer to pull the childNode, this is not the third input to createIndex. Rather, the input to createIndex is simply the parent node which we have pulled from the childNode. So it seems I am not feeding it an internalPointer at all, but simply something I have extracted from the internalPointer.

Why describe the third parameter as a pointer when it seems to just be an object (i.e., an item/node in your tree)? Given that I am working in Python, which does not use pointers, what is the third input (and for that matter what is an internalPointer anyway, in Python?).

Note crossposted at SO:
http://stackoverflow.com/questions/24763215/does-createindex-method-really-use-a-pointer

Infinity
15th July 2014, 17:15
The internal pointer is used by the model to associate the index with the internal data structure (from the docs).

It seems like you're doing something like the Simple Tree Model Example: http://qt-project.org/doc/qt-4.8/itemviews-simpletreemodel.html
Here the internal pointer points to a TreeItem instance which holds data for the index and also a pointer to the parent of the index (which points to another TreeItem instance of course). This pointer is used as internal pointer when creating the parent index.


Given that I am working in Python, which does not use pointers
Correct, in Python you don't use pointers (all values are references). Maybe it would make more sense to talk about "internal objects" here.

neuronet
15th July 2014, 17:20
Maybe this is a case where translation of documentation to Python hasn't been as smooth because we don't use pointers :)

(Note I just added to original post: I x-posted this to stack overflow)