<div dir="ltr"><br>So the code would be something like :<br><br># objects... like dicts<br><br>d = myQJSObject<br><br>if d[ "spices" ].toString() == "pebber" :<br> more = "yes please"<br><br><br>if d.property( "spices" ).toString() == "pebber" :<br> more = "yes please"<br><br><br><br><br># or for arrays <br><br>a = myQJSArray<br>nbr1 , nbr2 = a[0].toNumber() , a[2].toNumber()<br><br>vs.<br> <br>nbr1, nbr2 = a.property( 0 ).toNumber() , a.property( 1 ).toNumber()<br><br><br><br>Someone might then expect it could work with string??? <br>( which would fail.. )<br><br><br>s = myQJSString<br>firstLetter = s[0]<br><br><br>----<br><br>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, <br>I would probably start calling the items() or start iterate..... :-)<br><br><br>What I did was making a convert function, converting QJSValues to about the same form as the output had before.... <br>( unpack all QJSValues ... ) ... <br><br><br>My very untested version of the convert function is below, if someone should need something like that : <br><br>Best Regards<br><br>Brian ..<br><br><br><br>--------------------- 8< -----------------<br><br>from PyQt5.QtQml import QJSValueIterator, QJSValue<br>from functools import partial<br><br>def pyFyQJSValue( qjsValue, instance = None ):<br><br> if qjsValue.isString():<br> return qjsValue.toString()<br> elif qjsValue.isNumber():<br> return qjsValue.toNumber()<br> elif qjsValue.isArray():<br> return [ pyFyQJSValue( qjsValue.property( idx ) )<br> for idx in range( int( qjsValue.property( "length" ).toNumber() ) ) ]<br> elif qjsValue.isCallable():<br> return partial( _call, qjsValue, instance )<br> elif qjsValue.isQObject():<br> return qjsValue.toQObject()<br> elif qjsValue.isObject():<br> return dict( ( k, pyFyQJSValue( v, instance = qjsValue ) ) for k, v in iterJSObjectItems( qjsValue ) )<br> elif qjsValue.isBool():<br> return qjsValue.toBool()<br> elif qjsValue.isNull():<br> return None<br> elif qjsValue.isRegExp():<br> return qjsValue.toVariant()<br> elif qjsValue.isBool():<br> return qjsValue.toBool()<br> elif qjsValue.isDate():<br> return qjsValue.toDateTime()<br> elif qjsValue.isUndefined():<br> return None<br> elif qjsValue.isVariant():<br> return qjsValue.toVariant()<br><br> raise Exception( "sorry, you need to update the pyFyQJSValue to convert this unhandled value" )<br><br><br>def iterJSObjectItems( jsObject ):<br> if not jsObject.isObject():<br> return<br> it = QJSValueIterator( jsObject )<br> while it.hasNext():<br> it.next()<br> yield <a href="http://it.name">it.name</a>(), it.value()<br><br><br>def _call( qjsValue, instance, *args ):<br><br> qjsValueArgs = [ a if isinstance( a, QJSValue ) else QJSValue( a )<br> for a in args ]<br> if instance:<br> return qjsValue.callWithInstance( instance, qjsValueArgs )<br> else:<br> return qjsValue.call( qjsValueArgs )<br><br>---------------------- >8 ----------------<br><br><br> <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 5, 2015 at 8:04 AM, Phil Thompson <span dir="ltr"><<a href="mailto:phil@riverbankcomputing.com" target="_blank">phil@riverbankcomputing.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 03/01/2015 5:10 pm, B. B. wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hey Phil,<br>
<br>
The error had actually been easier explained making a short example from<br>
the beginning...<br>
<br>
Attatched is the sources for a very small program that demostrate what I<br>
mean.<br>
The introspection is done just by printing to the console.<br>
I print out in 2 sections - the first "makes sence" with the new source,<br>
the second makes sence with the old sources.<br>
<br>
You will notice in the second - with the old sources - the result from the<br>
"getStuff" function is python dicts and lists.<br>
<br>
Which if a much cleaner interface than the QJSValues the getStuff returns<br>
with the nevest sources.. :-)<br>
</blockquote>
<br></span>
It's a change in behaviour in Qt v5.4 - nothing to do with PyQt.<br>
<br>
I could implement the mapping protocol for QJSValue so that __getitem__ calls property(), __setitem__ calls setProperty() and __delitem__ calls deleteProperty(). Would that help?<span class="HOEnZb"><font color="#888888"><br>
<br>
Phil<br>
</font></span></blockquote></div><br></div>