[Development] QVariantMap <-> QML

Hausmann Simon Simon.Hausmann at theqtcompany.com
Sun Jun 14 19:57:40 CEST 2015


Hi,

This is a result of QVariant being a value type. If you make a copy and modify it, then the original remains as-is. The call to setContextProperty creates a copy. If you want explicitly shared data between the JavaScript environment and C++ then I would recommend using a JavaScript object - accessed from C++ as QJSValue, because those are explicitly shared.

Simon

  Original Message
From: Gerhard Scheikl
Sent: Sunday, June 14, 2015 19:43
To: development at qt-project.org
Subject: [Development] QVariantMap <-> QML


Hi

I recently discovered some unexpected behavior when exchanging a QVariantMap
with QML code:

Here's what I'm doing on the C++ side:
my_map = json_document.toVariant();
engine.rootContext()->setContextProperty("myMap", my_map);

Then on the QML side:
myMap.asdf.name = "asdf"

This change is not reflected in the original map.
Obviously, accessing asdf.name returns a copy.

Is this the intended behavior?
If so, how can I ensure that the data in C++ and on the QML side is always the
same?
(use QJSValue? use a QObject hierarchy with dynamic properties?)

Thanks!

Best regards
Gerhard
_______________________________________________
Development mailing list
Development at qt-project.org
http://lists.qt-project.org/mailman/listinfo/development



More information about the Development mailing list