[PyKDE] QModelIndex and PyQT
Torsten Marek
shlomme at gmx.net
Fri May 26 12:24:51 BST 2006
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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.
- - internalPointer() sometimes returns an invalid object which crashes the
interpreter. I'm investigating this right now...
best,
Torsten
- --
Torsten Marek <shlomme at gmx.net>
ID: A244C858 -- FP: 1902 0002 5DFC 856B F146 894C 7CC5 451E A244 C858
Keyserver: subkeys.pgp.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFEduWDfMVFHqJEyFgRAgBjAKCUOqauz/9McIoQKar0h4EGapcgoQCfQw4m
lnrmotdSr25/LNWQoa+1qCI=
=Akkc
-----END PGP SIGNATURE-----
More information about the PyQt
mailing list