pysideqmodelindex

Printing QModelIndex vs QModelIndex.model(): different hex values?


When you print out a QModelIndex in Pyside, the object representation shows the row, column, parent, model, and memory address. However, if you print out index.model(), the memory address for the model is different.

Here is some code that demonstrates what I mean:

from PySide import QtGui, QtCore

class TestQModelIndexModelWin(QtGui.QMainWindow):
    def __init__(self,  parent=None):
        super(TestQModelIndexModelWin, self).__init__(parent)

        self.listView = QtGui.QListView()
        self.setCentralWidget(self.listView)

        listModel = QtGui.QStringListModel(['foo', 'bar', 'baz'])
        self.listView.setModel(listModel)

        numItems = len(listModel.stringList())
        for i in range(numItems):
            index = listModel.index(i, 0)
            print index
            print index.model()

When running this code, the results look something like the following:

<PySide.QtCore.QModelIndex(0,0,0x0,QStringListModel(0xef1b7e0) )   at 0x0000000017656D08>
<PySide.QtGui.QStringListModel object at 0x0000000017656948>
<PySide.QtCore.QModelIndex(1,0,0x0,QStringListModel(0xef1b7e0) )   at 0x00000000176564C8>
<PySide.QtGui.QStringListModel object at 0x0000000017656948>
<PySide.QtCore.QModelIndex(2,0,0x0,QStringListModel(0xef1b7e0) )   at 0x0000000017656D08>
<PySide.QtGui.QStringListModel object at 0x0000000017656948>

Why does the QModelIndex show the QStringListModel hex value as 0xef1b7e0 but the QStringListModel shows its address as 0x0000000017656948?


Solution

  • The repr for index is showing the C++ address of the model it is associated with. Whereas the repr for index.model() is showing the address of the python object that wraps the C++ model.

    You can verify this by using the shiboken module:

    import shiboken
    ...
    
        print index
        print index.model()
        print shiboken.dump(index.model())
    

    which will produce output like this:

    <PySide.QtCore.QModelIndex(2,0,0x0,QStringListModel(0x17b0b40) )   at 0x7ff1a3715998>
    <PySide.QtGui.QStringListModel object at 0x7ff1a3715950>
    C++ address....... PySide.QtGui.QStringListModel/0x17b0b40
    hasOwnership...... 1
    containsCppWrapper 1
    validCppObject.... 1
    wasCreatedByPython 1