[PyQt] Re: memory corruption storing python objects in a QModelIndex with createIndex

Phil Thompson phil at riverbankcomputing.com
Wed Oct 1 12:53:07 BST 2008


On Tue, 30 Sep 2008 20:55:17 -0700, "Erick Tryzelaar"
<idadesub at users.sourceforge.net> wrote:
> On Tue, Sep 30, 2008 at 8:04 PM, Erick Tryzelaar
> <idadesub at users.sourceforge.net> wrote:
>> Could this possibly be the source of my trouble?
> 
> It doesn't appear to be. I modified it to use internalId and a global
> variable and it still segfaults. I've reduced this down as much as I
> can, does anyone have any idea why this keeps crashing?
> 
> 
> import sys
> from PyQt4.QtCore import *
> from PyQt4.QtGui import *
> 
> class Node:
>     def __init__(self, parent):
>         self.parent = parent
>         self.children = {}
> 
>     def child(self, row):
>         try:
>             return self.children[row]
>         except KeyError:
>             node = self.children[row] = Node(self)
>             return node
> 
> class Model(QAbstractItemModel):
>     def __init__(self, parent=None):
>         QAbstractItemModel.__init__(self, parent)
>         self.root = Node(None)
> 
>     def rowCount(self, parent=QModelIndex()):
>         return 1
> 
>     def columnCount(self, parent=QModelIndex()):
>         return 1
> 
>     def hasChildren(self, index):
>         return True
> 
>     def parent(self, index):
>         if not index.isValid():
>             return QModelIndex()
> 
>         parentNode = index.internalPointer().parent
>         if not parentNode:
>             return QModelIndex()
> 
>         return self.createIndex(0, 0, parentNode)
> 
>     def index(self, row, column, parent=QModelIndex()):
>         if column != 0:
>             return QModelIndex()
> 
>         if parent.isValid():
>             parentNode = parent.internalPointer()
>         else:
>             parentNode = self.root
> 
>         return self.createIndex(row, column, parentNode.child(row))
> 
>     def data(self, index, role=Qt.DisplayRole):
>         if not index.isValid() or role != Qt.DisplayRole:
>             return QVariant()
> 
>         return QVariant('a')
> 
> def main():
>     app = QApplication(sys.argv)
> 
>     model = Model()
> 
>     window = QTreeView()
>     window.setModel(model)
>     window.resize(QSize(600, 300))
>     window.show()
>     sys.exit(app.exec_())
> 
> main()

Try using modeltest.py from the contrib directory.

Phil


More information about the PyQt mailing list