Dictionary in QML - qt

I have a python code that sending a dictionary to my QML. I want to know how could I have keys of dictionary and length of dictionary in QML?

You can't actually send a Python Dict to QML. This is because QML does not have a Dict object type. It is most likely translated into a JSObject which can be manipulated like a normal Javascript Object. Refer to http://www.w3schools.com/js/js_objects.asp for information on working with JS objects.

Related

How can I serialize and deserialize a private type?

I'm working with the GNAT.OS_Lib module to implement a process manager for Linux. I'm using the subprograms in that module to spawn processes and get back PIDs, and now I want to save those PIDs to a file so they can be retrieved later to kill the process, get its status, etc. The problem is, the Process_Id type is private:
type Process_Id is private; -- s-os_lib.ads, line 743
The module provides a Pid_To_Integer function, but not the inverse. How can I serialize and deserialize Process_Ids?
Study section K.2 "Language Defined Attributes" in the Ada Language Reference Manual http://www.ada-auth.org/standards/12rm/html/RM-K-2.html
The attributes to study are S'Read, S'Write.
Your file must be created as a stream file. The Process_Id'Write attribute will serialize and write to the stream file. The Process_Id'Read attribute will read and de-serialize the data in the file.
If S'Read and S'Write do not work for you because of the nature of a compound type you should use the S'Input and S'Output attributes, which will read and write any bounds or discriminants.
S'Input and S'Output will work correctly with all types.

How can I load a list of defines into QML?

Here is the the thing, I have a header file with a list of defines, defining string constants. I'm doing my interface with QML. I was wondering if there is any way to import those defines so that I can reference from QML.

Flex Air: Read "ActionScript object" from file then cast?

It is quite simple to do it, you write the object down to file, then you read it:
http://corlan.org/2008/09/02/storing-data-locally-in-air/
http://cookbooks.adobe.com/post_Storing_ActionScript_Objects_in_the_Encrypted_Loca-10563.html
My questions are
why when we put [RemoteClass(alias="foo.Bar")] into VO, it can be
cast automatically (otherwise the type of the deserialized object is
Generic Object with correct properties data inside it)?
Is there another way to achieve it without RemoteClass tag? (Using metadata tag is preference)
Thanks
The answer is in the page you linked to:
Note that the alias is a key that is stored with the class instance
and links the class definition with the specific object that is stored
in the ByteArray when an instance of that object is serialized. This
key can be any unique string identifying this class, but convention is
to use the fully normalized package and class name.
That's why you get a generic object if you omit the alias - the deserialization method does not know what to do with the data, unless you specify to which class the values should be mapped.
Yes, there is: registerClassAlias() does the same thing. But the metadata tag is much prettier to read ;)

Qt: Pass pointer to QObject in QMimeData

Is it possible to pass a pointer to a QObject using QMimeData during drag-and-drop operation? QMimeData only has this function for storing data:
void QMimeData::setData(constQString &mimeType, const QByteArray &data)
but I can't find a way to safely encode a pointer into a QByteArray.
To clarify my goal: I need to pass a pointer to a QObject from a model to the target widget during the drag-and-drop operation.
Edit:
As far as I undrestand Mime data is all about passing application independent data from one place to another: urls, colors, html code. In my case I need to pass a pointer to a resource object eithin the application. How to you usually deal with this kind of drag-drops?
Thanks
Anton
You can subclass QMimeData and pass whatever you want.
To be on the safe & elegant side, I would come up with unique identifiers (e.g. strings, or numbers) for my objects and pass them as mime objects. Resolving a string back into the corresponding object using QHashmap is fast enough for your purpose.
The dirtiest (not recommended!) way would be that the identifier is the pointer address as int.
If you pass mime data with user interaction, you never know where it goes. If the user drops your pointer onto another application's window, it should fullfill the user's expectations best.
An application that gets a mangled up mime object and crashes for it is worst.
An application that gets a descriptive string and enables the user to understand what he was dropping is probably best.
I know that you can also, using the mime type, somewhat direct where the payload may be dropped and where not. the quintessence however is that you should stay within the mime concept. And that includes not passing a raw pointer.
As Kash said and the Qt docs suggest, subclass QMimeData.
Then, add the following:
text/plain data describing or representing the dragged object, so if you drop this data to notepad.exe, it results in something relevant
custom data type with an identifier or token that enables you to know that some dropped mime data is actually your subclass
add your own data in the subclass
You will still need to test the qobject_cast (or dynamic_cast), because some other program might have proxied your mime data object.

Flex HTTPService Resultformat

What is the real difference between these resultformats for HTTPService in Flex :
text
object
xml
e4x
Especially, the last three seem pretty close to each other by their description.
from the manual of HTTPService:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/rpc/http/HTTPService.html#resultFormat
object: The value returned is XML and is parsed as a tree of ActionScript objects. This is the default.
array: The value returned is XML and is parsed as a tree of ActionScript objects however if the top level object is not an Array, a new Array is created and the result set as the first item. If makeObjectsBindable is true then the Array will be wrapped in an ArrayCollection.
xml: The value returned is XML and is returned as literal XML in an ActionScript XMLnode object.
flashvars: The value returned is text containing name=value pairs separated by ampersands, which is parsed into an ActionScript object.
text: The value returned is text, and is left raw.
e4x: The value returned is XML and is returned as literal XML in an ActionScript XML object, which can be accessed using ECMAScript for XML (E4X) expressions.
The classtype of the returned object differs.
text => String
object => A generic object that you can use like a hash
e4x => an object of type XML
xml => I forget... a String?
I recently had some issues with the "object" and "e4x" resultFormat.
I have a base WebService Class that I use for sending requests and receiving results. By default, all results come back as "object". However, sometimes Flex looks at the data, and converts it to an appropriate type. For instance, if you have an XML result that looks like the following, it will convert it to an Array Object (not sure why...but...):
<root>
<child>text</child>
<child>text text</child>
</root>
Now, an Array Object like this can easily be cast as XML, since, as a string it is also XML.
However, some XML documents are returned as an ObjectProxy, which cannot be cast as XML, when the resultFormat is "object".
I tried using "e4x", as it was suggested here, but then I ran into problems with namespaces not being preserved correctly.
I finally tried "xml", and I am getting the expected results. It's interesting that when you inspect the event result property using the Flex Debugger, it is said to be an Array, even when you specify a resultFormat of "xml". I guess this allows for easily casting to ArrayCollection...not sure....

Resources