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

Erick Tryzelaar idadesub at users.sourceforge.net
Wed Oct 1 20:28:28 BST 2008


On Wed, Oct 1, 2008 at 4:53 AM, Phil Thompson
<phil at riverbankcomputing.com> wrote:
> Try using modeltest.py from the contrib directory.

Thanks again Phil. That found some problems, but I'm still getting the
aborts. I'm now getting "Fatal Python error: GC object already
tracked" with this new script. I've marked where the GC exception
occurs:


#!/usr/bin/python2

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

import modeltest

class Node:
    def __init__(self):
        self.children = {}

    def child(self):
        try:
            print 'before bug'
            node = self.children[0]
            print 'after bug'
        except KeyError:
            node = self.children[0] = Node()

        return node

class Model(QAbstractItemModel):
    def __init__(self, parent=None):
        QAbstractItemModel.__init__(self, parent)

    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()

        node = Node()
        return self.createIndex(0, 0, node)

    def index(self, row, column, parent=QModelIndex()):
        if row != 0 or column != 0:
            return QModelIndex()

        if parent.isValid():
            parentNode = parent.internalPointer().child()
        else:
            node = Node()

        return self.createIndex(row, column, node)

    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()
    test = modeltest.ModelTest(model, model)

    window = QTreeView()
    window.setModel(model)
    window.resize(QSize(600, 300))
    window.show()
    sys.exit(app.exec_())

main()


More information about the PyQt mailing list