在the 4.8 docs , 规定:
When using files with relative paths, the path should be relative to the file whereQt.createComponent() is executed.
所以我使用下面的方法来创建一个对象:
Qt.createComponent("./foo/bar.qml").createObject(_this)
但是我得到了错误:
QQmlComponent: Component is not ready
我也试过(运气不好)"foo/bar.qml"
。更改为 "foobar.qml"
工作正常,目录结构为:
./
+--foo/
| `--bar.qml
`--foobar.qml
但是我注意到 the version 5 docs 中没有提及相对 URL ,那么有没有一种新的方法可以做到这一点,我错过了?
最佳答案
我怀疑这与相对路径直接相关。 “组件未就绪”错误意味着您在组件就绪之前调用了 createObject()
。相反,您需要监听组件的 statusChanged
信号,并仅在状态更改为 Component.Ready
时创建对象。
看看Dynamic QML Object Creation from JavaScript获取完整示例和更多详细信息。
关于Qt 从相对 URL 创建组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26446017/