[PyKDE] QModelIndex and PyQT

Phil Thompson phil at riverbankcomputing.co.uk
Thu May 25 10:04:01 BST 2006


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




More information about the PyQt mailing list