- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
属性变体 a:{}
似乎不起作用。 a
最终未定义,而不是一个空字典。
我对 Javascript 不是很有经验...初始化属性以保存空字典的正确方法是什么?
以下 qml 在控制台上打印“qrc:/main.qml:13: TypeError: Type error”。但是,如果 a
被初始化为 {"dummyentry": 42}
,那么记录预期的结果。
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
ApplicationWindow {
visible: true; width: 600; height: 200
property variant a: {}
Component.onCompleted: {
console.log("initial a="+JSON.stringify(a)) // TypeError: Type error
a["newkey"] = 999 // gets "TypeError: Type error"
console.log("updated a="+JSON.stringify(a))
}
}
最佳答案
可以引用Qt documentation :
The QML syntax defines that curly braces on the right-hand-side of a property value initialization assignment denote a binding assignment. This can be confusing when initializing a var property, as empty curly braces in JavaScript can denote either an expression block or an empty object declaration. If you wish to initialize a var property to an empty object value, you should wrap the curly braces in parentheses.
因此,当使用 property var a:{}
时,大括号被解释为一个空表达式,其结果是未定义的。
通过使用 property var a: ({})
语法,您将创建一个空的 JS 对象。您可以向它添加属性和方法,但只需添加一组属性,它就可以像字典一样工作。
您可以通过两种方式访问对象属性:
objectName.propertyName
objectName["propertyName"]
在这里您可以找到有关 JS 对象的更多信息:link .
关于qml - 无法将 qml 属性初始化为 {},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46627883/
出于好奇,我尝试了一些原型(prototype)制作,但似乎只允许在第一个位置使用子例程的原型(prototype) &。 当我写作时 sub test (&$$) { do_somethin
我需要开发一个类似于 Android Play 商店应用程序或类似 this app 的应用程序.我阅读了很多教程,发现几乎每个教程都有与 this one 类似的例子。 . 我已经开始使用我的应用程
考虑一个表示“事件之间的时间”的列: (5, 40, 3, 6, 0, 9, 0, 4, 5, 18, 2, 4, 3, 2) 我想将这些分组到 30 个桶中,但桶会重置。期望的结果: (0, 1,
我是一名优秀的程序员,十分优秀!