[PyQt] Conversion from QML

Phil Thompson phil at riverbankcomputing.com
Wed Jan 7 14:39:18 GMT 2015


On 07/01/2015 2:24 pm, B. B. wrote:
> So the code would be something like :
> 
> # objects... like dicts
> 
> d = myQJSObject
> 
> if d[ "spices" ].toString() == "pebber" :
>     more = "yes please"
> 
> 
> if d.property( "spices" ).toString() == "pebber" :
>      more = "yes please"
> 
> 
> 
> 
> # or for arrays
> 
> a = myQJSArray
> nbr1 , nbr2 = a[0].toNumber() , a[2].toNumber()
> 
> vs.
> 
> nbr1, nbr2 = a.property( 0 ).toNumber() , a.property( 1 ).toNumber()
> 
> 
> 
> Someone might then expect it could work with string???
> ( which would fail.. )
> 
> 
> s  = myQJSString
> firstLetter = s[0]
> 
> 
> ----
> 
> I think it looks better with the mapping protocol, but personally in my
> code, I would always use the property function. This is only because I 
> know
> my memory, so when I would have to fix an error 7 months after writing,
> I would probably start calling the items() or start iterate..... :-)
> 
> 
> What I did was making a convert function, converting QJSValues to about 
> the
> same form as the output had before....
> ( unpack all QJSValues ... ) ...

Didn't you always have to call toString() etc. to get the actual values, 
even with Qt v5.3?

If so then implementing the mapping protocol should allow you to write 
code that would work with both v5.3 and v5.4 without needing to be 
changed.

Phil


More information about the PyQt mailing list