[PyQt] Sip usage problem

Hans-Peter Jansen hpj at urpla.net
Thu Dec 2 00:10:24 GMT 2010


On Wednesday 01 December 2010, 23:45:44 James Polk wrote:
> I read the Wikipedia page that explains what SIP is and does,
> though I can't say I understood everything it explained ;-)
>
> I'm getting a SIP usage error and I was hoping someone here
> could help me understand the phenomena...
>
> In my program (which borrows heavily from the BasicSortFilter
> example) near the top, I have the line
>
>     sip.setapi('QVariant', 2)
>
> My limited understanding is that this is doing some kind of
> type mapping,...or type casting (pardon the pun)...
>
> Apparently, this statement has bearing on the "filterRegExpChanged"
> function.
>
> Now the code I'm writing gets import into Maya, which as probably
> many know, is now fully Python/Qt/PyQt compliant  (well sorta', lol)
> So when I launch my script, I get an error which I'm assuming
> indicates that somewhere during Maya's own launch, it does a
>
>     sip.setapi('QVariant', 1)
>
> ...but that's speculation....Maya's error message is:
>
> # Error: API 'QVariant' has already been set to version 1

The API must be set before the first PyQt4 import, otherwise it is set 
implicitely to API 1 for Python 2 (in order to keep backwards 
compatibility).

> Now, I've kinda' found a hack around this to get it to actually work
> in Maya, but I'm worried I might be adversely affecting other PyQt
> scripts,etc...and I'd to understand what's going on more...
>
> I'd be very grateful if someone could explain what this little
> statement is doing behind the scenes...

In simple words, it eliminates QVariants, which are a crude way to use 
dynamically typed values in a statically typed language. Hence, nothing 
we should care about in Python.

With QVariant API V.1, you would need to wrap any types you use into 
QVariants where they are expected from Qt, which even hurts more than 
in C++, since the compiler is able to do implicit casts. This is not 
possible in Python of course.

Consequently, you need to peel the values out of QVariants 
with .toPyObject() on the other way around, which is no fun either. 

Therefor, V.2 QVariants are a major improvement for using PyQt in a more 
pythonic way, and nobody should miss this opportunity in any new 
project. 

Although Phil has done a pretty genius job, it should be noted, that, 
hmm, its implementation is still improving for some special/corner 
cases.. ;-)

Check out the other V.2 APIs available:

http://www.riverbankcomputing.com/static/Docs/PyQt4/pyqt4ref.html#selecting-incompatible-apis

Pete


More information about the PyQt mailing list