[PyQt] Using QVariant.toPyObject to wrap the to* functions

Arve Knudsen arve.knudsen at gmail.com
Thu Aug 14 08:44:20 BST 2008

On Wed, Aug 13, 2008 at 9:37 PM, Erick Tryzelaar
<idadesub at users.sourceforge.net> wrote:
> Hello again,
> In our code right now, we keep having to extract data from a QVariant,
> and we end up with a lot of copies of:
> if v.type() == QVariant.Int:
>  ...
> elif v.type() == QVariant.String:
>  ...
> ...
> We don't really care exactly what the type is, but we have to do this
> in order to work directly with the values. Since there's already a
> QVariant.toPyObject that can extract arbitrary python objects, it
> seems like it wouldn't be that much extra work to extend it to be able
> to extract all the QVariant standard types as well. I'd guess that the
> implementation could just be abstracting out the first stage of
> translation, like this:
> QVariant(1).toPyObject() == QVariant(1).toInt()[0]
> QVariant(QDateTime(...)).toPyObject() ==
> QVariant(QDateTime(...)).toDateTime()[0]
> and etc. This makes QVariant a little more pythonic to me, in my opinion.

I agree about this. It would be more natural to have toPyObject work
for the standard types in addition to custom ones, and simplify the
logic greatly.


More information about the PyQt mailing list