Validation / access to dynamically created objects in QML

Is there a way to check for existence and access a dynamically created object in QML / javascript (without using C ++)?

I am trying to create an application with an interface similar to a map - taking into account the key and the object, my application should find out if the object exists with the key and overwrite the new object. If this is not the case, the application should create a new object and associate it with a key.

The documentation says dynamic-managed objects have no identifiers, and the only way I found access to them is to use objectName, which seems to require a C ++ application.

early.

+4
source share
1 answer

You can use a JavaScript object as a map. You cannot directly manipulate it in QML, but you can move all the code to process this object into a JavaScript file and include it as a module. Here is a simple example:

Map.js:

var _map = new Object() function value(key) { return _map[key] } function setValue(key, value) { _map[key] = value } function remove(key) { delete _map[key] } function keys() { return Object.keys(_map) } function process() { for (var key in _map) { /* do something */ } } 

QML example:

 import QtQuick 1.1 import "Map.js" as Map Item { Component.onCompleted: { Map.setValue("test", "hello") console.log("test = ", Map.value("test")) Map.remove("test", "hello") console.log("test = ", Map.value("test")) } } 

The output will be:

 test = hello test = undefined 
+4
source

All Articles