我目前正在制作一款名为 Snowflake 的游戏,以学习如何使用 Javascript 对 QML 进行编程。
- 我的源代码在这里:https://gist.github.com/4510190 .
除此之外我得到的唯一文件是默认的 qmlproject 文件和 png 文件。
我已经制作了这个计时器 (Timer) 和一个矩形 (skullflake),它应该在它完成的每个时间间隔内生成。
Timer { //spawnrate
interval: 2000; running: true; repeat: true
onTriggered: Qt.createQmlObject(skullflake)
}
但是当我尝试运行它时,它给了我错误:
file:///where/the/project/is/saved/Snowflake/Snowflake.qml:21: Error: Qt.createQmlObject(): Invalid arguments
我试过这两个元素,但我似乎无法让它工作
Qt.createComponent(skullflake)
Qt.createQmlObject(skullflake)
最佳答案
您使用了错误的 Qt.createQmlComponent()。 它需要 3 个参数:
Qt::createQmlObject ( string qml, object parent, string filepath )
所以它应该看起来像这样(父元素应该是包含 skullflake 的元素的 id):
Timer { //spawnrate
interval: 2000; running: true; repeat: true
onTriggered: Qt.createQmlObject("YOUR skullflake QML CODE (NOT FILE PATH)", parent, "skullflake")
}
关于javascript - 如何生成 QML 矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14277880/