Hi again!
I am about to give up now, but first I want to post my current code again; I
added some assertions which show an interesting fact which I have no
explanation for: One assertion fails, since I am getting a QModelIndex of my
proxy model in which internalPointer does *not* contain a QModelIndex of the
proxied model anymore, but NULL/None.
This is very strange, and with persistent QMIs, it crashes completely.
Can you confirm this, or do you know why this is so?
Maybe it is a bug/limitation of PyQt, and QMI's internalPointer() does not
increment the refcount? That would explain a lot...
This is with:
python 2.6.5 (release26-maint, Aug 18 2010, 09:33:34)
sip 4.10.5
PyQt4 4.7 (using Qt 4.6.2)
Have a nice day,
Hans
from PyQt4 import QtCore, QtGui
# I tried two variants, setting this to True could be even safer:
needPersistentModelIndex = False
class DummyProxyModel(QtCore.QAbstractItemModel):
def __init__(self, baseModel, parent = None):
QtCore.QAbstractItemModel.__init__(self, parent)
self._baseModel = baseModel
def _baseIndex(self, index):
"""Map an index of this model back to the _baseModel."""
if not index.isValid():
return QtCore.QModelIndex()
assert index.model() is self
print "_baseIndex got %r" % index
baseIndex = index.internalPointer()
print " pointing to %r" % (baseIndex, )
if not baseIndex.isValid():
return QtCore.QModelIndex()
assert baseIndex.model() is self._baseModel
if needPersistentModelIndex:
# convert QPersistentModelIndex into QModelIndex:
baseIndex = self._baseModel.index(
baseIndex.row(), baseIndex.column(), baseIndex.parent())
return baseIndex
def index(self, row, column, parent = QtCore.QModelIndex()):
baseParent = self._baseIndex(parent)
baseIndex = self._baseModel.index(row, column, baseParent)
if needPersistentModelIndex:
baseIndex = QtCore.QPersistentModelIndex(baseIndex)
assert baseIndex.model() is self._baseModel
result = self.createIndex(row, column, baseIndex)
print "index() created %r" % result
print " pointing to %r" % (result.internalPointer(), )
return result
def parent(self, index):
baseIndex = self._baseIndex(index)
print type(baseIndex)
baseParent = self._baseModel.parent(baseIndex)
if not baseParent.isValid():
return QtCore.QModelIndex()
if needPersistentModelIndex:
baseParent = QtCore.QPersistentModelIndex(baseParent)
assert baseParent.model() is self._baseModel
result = self.createIndex(
baseParent.row(), baseParent.column(), baseParent)
print "parent() created %r" % result
print " pointing to %r" % (result.internalPointer(), )
return result
def rowCount(self, index):
baseIndex = self._baseIndex(index)
return self._baseModel.rowCount(baseIndex)
def columnCount(self, index):
baseIndex = self._baseIndex(index)
return self._baseModel.columnCount(baseIndex)
def data(self, index, role):
baseIndex = self._baseIndex(index)
return self._baseModel.data(baseIndex, role)
# --------------------------------------------------------------------
# improve debug output
# --------------------------------------------------------------------
def minimal_repr(obj, *details):
if obj is None:
return "None"
className = type(obj).__name__
if className.startswith("PyQt4."):
className = className[6:]
className = className.replace("QtCore.", "")
className = className.replace("QtGui.", "")
return "<%s>" % (
", ".join(d for d in (["%s at 0x%0xd" % (className, id(obj))] + map(str, details)) if d))
def QModelIndex__repr__(index):
details = ["(%d, %d)" % (index.row(), index.column()),
"belonging to %s" % minimal_repr(index.model())]
if not index.isValid():
details.append("INVALID")
return minimal_repr(index, *details)
QtCore.QModelIndex.__repr__ = QModelIndex__repr__
QtCore.QPersistentModelIndex.__repr__ = QModelIndex__repr__
# --------------------------------------------------------------------
# test code (__main__)
# --------------------------------------------------------------------
app = QtGui.QApplication([])
base = QtGui.QStringListModel(map(str, range(10)), app)
model = DummyProxyModel(base, app)
assert model.rowCount(QtCore.QModelIndex()) == base.rowCount(QtCore.QModelIndex())
# trivial test of base model (no problems here)
i = base.index(0, 0)
assert i.isValid()
assert not i.parent().isValid()
# create model index:
i = model.index(0, 0)
print "__main__ got %r" % (i, )
assert i.isValid()
assert i.internalPointer().isValid()
assert not i.parent().isValid()
print
j = model._baseIndex(i)
print "3) got base index", j
_______________________________________________
PyQt mailing list [email protected]
http://www.riverbankcomputing.com/mailman/listinfo/pyqt