[PyKDE] QModelIndex and PyQT

Phil Thompson phil at riverbankcomputing.co.uk
Fri May 26 12:40:03 BST 2006


On Friday 26 May 2006 12:24 pm, Torsten Marek wrote:
> Phil Thompson wrote:
> > On Wednesday 24 May 2006 10:39 pm, Allen Bierbaum wrote:
> >> I apologize if this has been discussed here before, I searched the
> >> mailing list and didn't find anything.
> >>
> >> I am beginning to use PyQT4 and I have run into some major headaches
> >> trying to use QModelIndex.  The problem stems from the fact that as
> >> far as I can tell with the PyQT bindings it is not possible to attach
> >> a reference to a python object to the index to then later be used by
> >> data(), parent(), or any other methods.
> >>
> >> In C++ this is possible (and seems to be recommended usage) by using
> >> the internalPointer() method of ModelIndex.  This works for C++
> >> because the internal pointer is stored as a void pointer that the
> >> developer knows the true type of and they can just cast it to the
> >> correct type to use it.
> >>
> >> Without this capability all use of QModelIndex in PyQT, when I use
> >> TreeViews I have found myself contorting my data models quite a bit to
> >> make things limp along.   I am currently  funneling all index lookups
> >> through a manually maintained map from int id's to python objects.  Is
> >> this how other people are also working with QT4 models?
> >>
> >> I was surprised that there was no way (that I know of) to attach a
> >> python object to a QModelIndex.  It seems from the QT documentation
> >> that this is the intent of how indexes are to be used in C++, but
> >> there is no corresponding way to do this in Python.
> >>
> >> I was wondering though, would it be possible to expand the PyQT
> >> bindings a bit and add two methods:
> >>
> >> QModelIndex.internalPyObj()
> >> QAbstractItemModel.createIndexPy(int, int, pyobj)
> >>
> >> I think it could be implemented by casting from PyObject* to void*
> >> behind the scenes and passing through to the existing
> >> internalPointer() C++ interface.  This would make working in Python
> >> much easier while still preserving the intent of the QModelIndex
> >> interface.
> >>
> >> Any comments?  Am I missing some easy way to do this already?
> >
> > You are quite right, but there is no need to add new methods as the
> > existing methods are useless in a Python context so they can be re-used.
> >
> > In tonight's snapshot both will accept/return Python objects rather than
> > void *. You will still need to keep an external reference to any objects.
> >
> > Phil
>
> Hi Phil,
>
> I really like that feature, however, there are some problems with it right
> now: - it's impossible to use IDs instead of PyObject (unless one keeps a
> reference to the int, which is rather clumsy), since everything is a
> PyObject. Changing the order of the two createIndex(...) overloads remedies
> this.

Good point.

> - internalPointer() sometimes returns an invalid object which crashes the
> interpreter. I'm investigating this right now...

Unless I've done something particularly stupid, that can only be because the 
object has been garbage collected.

Phil




More information about the PyQt mailing list