[PyQt] Construct QVariant from object of user type

Arve Knudsen arve.knudsen at gmail.com
Fri Apr 18 14:15:47 BST 2008


On 4/18/08, Phil Thompson <phil at riverbankcomputing.co.uk> wrote:
> On Friday 18 April 2008, Arve Knudsen wrote:
>  > On 4/17/08, Arve Knudsen <arve.knudsen at gmail.com> wrote:
>  > > On 4/17/08, Matt Newell <newellm at blur.com> wrote:
>  > >  > On Wednesday 16 April 2008 14:18:26 Arve Knudsen wrote:
>  > >  >  > On Wed, Apr 16, 2008 at 11:33 AM, Phil Thompson
>  > >  >  >
>  > >  >  > <phil at riverbankcomputing.co.uk> wrote:
>  > >  >  > > On Wednesday 16 April 2008, Arve Knudsen wrote:
>  > >  >  > >  > Phil, any comment on this?
>  > >  >  > >  >
>  > >  >  > >  > Thanks,
>  > >  >  > >  > Arve
>  > >  >  > >
>  > >  >  > >  Unless you can use the ctor that takes a void* I don't see how
>  > >  >  > > you can expect to extend the functionality of a C++ class from
>  > >  >  > > Python.
>  > >  >  >
>  > >  >  > How am I supposed to use the QVariant(int typeOrUserType, const
>  > >  >  > void* copy) constructor from Python? The documentation refers to
>  > >  >  > sip.voidptr, which I know nothing about, and  to use
>  > >  >  > qVariantFromValue which isn't defined.
>  > >  >  >
>  > >  >  > I need to store objects of a custom class in QVariants, with a
>  > >  >  > certain type code (QVariant::Type). The reason I need to do this is
>  > >  >  > that QItemEditorFactory is parameterized on QVariant::Type.
>  > >  >  >
>  > >  >  > Arve
>  > >  >
>  > >  > If you look at qmetatype.h, you'll see that it should be possible to
>  > >  > create a mechanism to register custom python classes as QVariant
>  > >  > types.  You just need to implement Constructor/Destructor methods that
>  > >  > call Py_INCREF/Py_DECREF. Then for each custom python type call
>  > >  > QMetaType::registerType(...).  This would need to be implemented in
>  > >  > c++ with a python interface.
>  > >  >
>  > >  >  You could then write a custom QVariant constructor that detects if
>  > >  > the python object's type is registered, and automatically call the
>  > >  > QVariant(int type,void*) ctor, or throws an exception for
>  > >  > non-registered types.
>  > >
>  > > So I would *have to* do this in C++? Ugh.
>  >
>  > Phil: Can you confirm that I have to do this via C++?
>
>
> I thought I already had in the above.

Perhaps you did, but your reply was too terse to make much sense to
me, especially since my goal is not to extend a C++ class but to store
instances of a Python class.

Your reply might make immediate sense for someone familiar with
QVariant's workings - I, however, am not.

Arve


More information about the PyQt mailing list