通过JavaScript创建Qml对象
2014-09-22 10:38
190 查看
有两种方法可以创建,都是全局对象Qt提供的方法 一:用Qt.createComponent加载一个qml文件并创建Component 二:用Qt.createQmlObject从一个qml字符串创建Component 注意,以上两种方法返回的是Component,Component在QML中是一种类型,参考文档: http://qt-project.org/doc/qt-5/qml-qtqml-component.html#details 因此还要调用Component的createObject来创建真正可用的对象。createObject第一个参数是指定挂在谁的下方,也就是父窗口是谁。如果传递null,则代表暂时不显示。 如果要销毁,只需要调用对象的destroy方法即可。 调用createObject方法需要注意qml文件已经被加载完成才行,因为这种机制允许远程加载qml文件,所以需要检查Component的status属性: This property holds the status of component loading. The status can be one of the following:
Component.Null - no data is availablefor the component
Component.Ready - the component has been loaded, and can be used to create instances.
Component.Loading - the component is currently being loaded
Component.Error - an error occurredwhile loading the component. Calling errorString() will provide a human-readable description of any errors.
if (itemComponent.status == Component.Ready && draggedItem == null) {
draggedItem = itemComponent.createObject(
window,
{
"image": paletteItem.image,
"x": posnInWindow.x,
"y": posnInWindow.y,
"z": 3
}
);
// make sure created item is above the ground layer
} else if (itemComponent.status == Component.Error) {
draggedItem = null;
console.log("error creating component");
console.log(itemComponent.errorString());
}
} 注意在JavaScript中没有真正的类型,类型也是由对象模拟的。所以Qml 的Component在JavaScript代码中也表现为一个对象,比如上面代码的itemComponent就是Qml的Component,但也是一个对象。用它的createObject再创建真正可用的对象挂在window对象下面。 官方文档参考: http://qt-project.org/doc/qt-4.8/qdeclarativedynamicobjects. href="http://www.2cto.com/kf/qianduan/css/" target=_blank>html http://qt-project.org/doc/qt-5/qml-qtqml-qt.html#createComponent-method http://qt-project.org/doc/qt-5/qml-qtqml-qt.html#createQmlObject-method 这就和web开发用JavaScript动态创建HTML的tag并插入到DOM模型中很像了。
Component.Null - no data is availablefor the component
Component.Ready - the component has been loaded, and can be used to create instances.
Component.Loading - the component is currently being loaded
Component.Error - an error occurredwhile loading the component. Calling errorString() will provide a human-readable description of any errors.
This property holds the status of component loading. The status can be one of the following:
Component.Null - no data is available
for
the component
Component.Ready - the component has been loaded, and can be used to create instances.
Component.Loading - the component is currently being loaded
Component.Error - an error occurred
while
loading the component. Calling errorString() will provide a human-readable description of any errors.下面是例子代码: function createItem() {
if (itemComponent.status == Component.Ready && draggedItem == null) {
draggedItem = itemComponent.createObject(
window,
{
"image": paletteItem.image,
"x": posnInWindow.x,
"y": posnInWindow.y,
"z": 3
}
);
// make sure created item is above the ground layer
} else if (itemComponent.status == Component.Error) {
draggedItem = null;
console.log("error creating component");
console.log(itemComponent.errorString());
}
} 注意在JavaScript中没有真正的类型,类型也是由对象模拟的。所以Qml 的Component在JavaScript代码中也表现为一个对象,比如上面代码的itemComponent就是Qml的Component,但也是一个对象。用它的createObject再创建真正可用的对象挂在window对象下面。 官方文档参考: http://qt-project.org/doc/qt-4.8/qdeclarativedynamicobjects. href="http://www.2cto.com/kf/qianduan/css/" target=_blank>html http://qt-project.org/doc/qt-5/qml-qtqml-qt.html#createComponent-method http://qt-project.org/doc/qt-5/qml-qtqml-qt.html#createQmlObject-method 这就和web开发用JavaScript动态创建HTML的tag并插入到DOM模型中很像了。
相关文章推荐
- 通过JavaScript创建Qml对象
- JavaScript 通过function创建对象的思考
- javascript中创建对象,并序列化为json,通过$.ajax的提交json数据到后台方法,取值为null的问题
- java 和 JavaScript都可以在创建一个对象时,就可以通过这个对象调用相应方法
- javascript入门·Document对象入门讲解(访问表单,创建新页,获取页标题)
- 在JavaScript中创建新对象
- JavaScript创建高级对象的方法
- 通过创建 HttpCookie 对象的实例编写 Cookie
- javascript入门·Document对象入门讲解(访问表单,创建新页,获取页标题)
- JavaScript 创建自己的对象
- 用javascript 创建ActiveX对象时出现:Automation server can't create object"(Automation 服务器不能创建对). 错误的解决办法。
- Javascript 创建对象的两种方法
- 使用JavaScript创建一个字符串对象
- JavaScript中创建对象
- javascript实例教程(5) 利用javascript创建对象
- 使用JavaScript创建一个字符串对象
- javaScript--创建对象
- JavaScript 中创建自定义对象
- 五 在JavaScript中创建新对象
- javascript 创建高级对象