使用 JavaScript 修改 QVariantMap
QtWebKit Bridge 文档声明如下 -
复合 (JSON) 对象 JavaScript 复合对象,也称为 JSON 对象,是保存一个变量 键值对列表,其中所有 键是字符串,值可以 有任何类型。这个翻译得很 以及 QVariantMap,这没什么 不仅仅是 QString 的 QMap Q变体。无缝转换 JSON 对象和 QVariantMap 之间 提供了一种非常方便的方式 传递任意结构化数据 C++ 和 JavaScript 之间 环境。本机 QObject 必须 确保复合值是 转换为 QVariantMaps 和 QVariantLists,JavaScript 是 保证收到它们 有意义的方式。请注意,键入 JSON 不支持,例如 JavaScript 函数和 getters/setters 不会被转换。
这是否意味着,虽然 JavaScript 能够读取 QVariantList,但无法修改它?
我尝试添加 getter 和 setter 来进行测试 -
Q_PROPERTY( QVariantMap Settings READ GetShadowSettings WRITE SetShadowSettings )
当 JavaScript 想要访问 QVariantMap 中的任何数据时,将调用 getter 函数。不幸的是,当 JavaScript 尝试更新 QVariantMap 时,会再次调用 getter 函数(而不是 setter 函数)。
我可以使用简单的辅助函数来修改数据,例如 -
Q_INVOKABLE void Update( QString key, QVariant value ) {
settings[key] = value;
}
我只是想知道是否有一种方法可以在不需要辅助函数的情况下执行此操作?
The QtWebKit Bridge documentation states the following -
Compound (JSON) objects JavaScript
compound objects, also known as JSON
objects, are variables that hold a
list of key-value pairs, where all the
keys are strings and the values can
have any type. This translates very
well to QVariantMap, which is nothing
more than a QMap of QString to
QVariant. The seamless conversion
between JSON objects and QVariantMap
allows for a very convenient way of
passing arbitrary structured data
between C++ and the JavaScript
environment. The native QObject has to
make sure that compound values are
converted to QVariantMaps and
QVariantLists, and JavaScript is
guaranteed to receive them in a
meaningful way. Note that types that
are not supported by JSON, such as
JavaScript functions and
getters/setters, are not converted.
Does this mean that, while JavaScript is able to read a QVariantList, it is unable to modify it?
I've tried adding a getter and setter for test purposes -
Q_PROPERTY( QVariantMap Settings READ GetShadowSettings WRITE SetShadowSettings )
The getter function is being called when the JavaScript wants to access any data from the QVariantMap. Unfortunately, when the JavaScript attempts to update the QVariantMap, the getter function is called again (rather than the setter function).
I can modify the data using a simple helper function such as -
Q_INVOKABLE void Update( QString key, QVariant value ) {
settings[key] = value;
}
I was just wondering if there was a way of doing this without the need for a helper function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对 PhantomJS 使用 QVariantMap ,它工作得很好。例如, WebPage#viewportSize 只是其中的 QVariantMap执行。通常的问题是您不能尝试仅更新其属性之一,例如
viewportSize.width = 300
。您必须传回一个对象,例如:viewportSize = { width: 300, height: 200 }
。如果您需要能够做到前者,唯一(丑陋的)可能有效的解决方法是创建一个辅助对象,例如上述情况下的
Size
,它具有适合个人的 setter 和 getter财产并处理桥梁的内务管理。I use QVariantMap for PhantomJS and it works just fine. For example, WebPage#viewportSize is just QVariantMap in its implementation. The usual problem is you can't try to update one of its property only, e.g.
viewportSize.width = 300
. You have to pass back an object, e.g.:viewportSize = { width: 300, height: 200 }
.If you need to able to do the former, the only (ugly) workaround that might work is to create a helper object, e.g.
Size
in the above case, which has the proper setter and getter for the individual property and handle the housekeeping of bridging.