通过JavaScript创建Qml对象
有两种方法可以创建,都是全局对象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 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.
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.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创建自定义对象和prototype
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JavaScript 创建类/对象的几种方式
在JS中,创建对象(Create Object)并不完全是我们时常说的创建类对象,JS中的对象强调的是一种复合类型,JS中创建对象及对对象的访问是极其灵活的. JS对象是一种复合类型,它允许你通过变量 ...
- JavaScript—创建正则对象
创建正则对象 方式1: var reg = new RegExp('\d', 'i');var reg = new RegExp('\d', 'gi'); 方式2: var reg = /\d/i;v ...
- JavaScript创建Map对象(转)
JavaScript 里面本身没有map对象,用JavaScript的Array来实现Map的数据结构. /* * MAP对象,实现MAP功能 * * 接口: * size() 获取MAP元素 ...
- 【微信小程序开发之坑】javascript创建date对象
最近开发中用到date,开始以如下方式来创建: var date = new Date('2018-01-30 11:00:00'); 在开发工具上,调试,ios 和 android都好好的. 在真机 ...
- Qt Quick 组件和动态创建的对象具体的解释
在<Qt Quick 事件处理之信号与槽>一文中介绍自己定义信号时,举了一个简单的样例.定义了一个颜色选择组件,当用户在组建内点击鼠标时,该组件会发出一个携带颜色值的信号,当时我使用 Co ...
- Ajax 学习之创建XMLHttpRequest对象------Ajax的核心
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- JavaScript之ECMA对象的学习
从传统意义上来说,ECMAScript 并不真正具有类.事实上,除了说明不存在类,在 ECMA-262 中根本没有出现“类”这个词.ECMAScript 定义了“对象定义”,逻辑上等价于其他程序设计语 ...
- js如何创建JSON对象
js如何创建JSON对象 一.总结 一句话总结:直接创建js数组和js对象即可,然后JSON.stringify就可以获取json字符串,js中的一切都是对象,而且js中的对象都是json对象 js ...
随机推荐
- 用servlet校验密码
一.结果图 package Login; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connect ...
- VS2013下使用cjson
想要在C++实现json文件的读取.因为中间也遇到过很简单的坑,为了增加记忆,对实现过程做一个记录. 本文采用的是静态链接库的方式: 1.先在github上下载源码, json源码下载地址 2.打开m ...
- linux下VI模式中上下左右键和回退键出现字母
1.编辑/etc/vim/vimrc.tiny 由于/etc/vim/vimrc.tiny的拥有者是root用户,所以要在root的权限下对这个文件进行修改.很简单,这个文件里面的倒数第二句话是“se ...
- HDFS HA和Federaion
1.HA HA即为High Availability,用于解决NameNode单点故障问题,该特性通过热备的方式为主NameNode提供一个备用者,一旦主NameNode出现故障,可以迅速切换至备Na ...
- JVM的内存分配和回收策略
对象的Class加载 虚拟机遇到一条new指令时,首先将去检查这个指令的参数是否能在常量池中定位到一个类的符号引用,并且检查这个符号引用代表的类是否已被加载.解析和初始化过.如果没有,那必须先执行相应 ...
- Spring Boot学习笔记-配置devtools实现热部署
写在前面 Spring为开发者提供了一个名为spring-boot-devtools的模块来使Spring Boot应用支持热部署,提高开发者的开发效率,无需手动重启Spring Boot应用. de ...
- EF 连接到 Azure-SQL
using Autofac; using Autofac.Integration.Mvc; using System; using System.Collections.Generic; using ...
- 搭建简单FTP
搭建简单FTP 环境 CentOS 7 安装 yum install vsftpd 修改配置文件, 在/etc/vsftpd/vsftpd.conf中添加allow_writeable_chroot= ...
- 使用webgl(three.js)创建3D机房(升级版)-普通机房
序: 目前市面上的数据中心主要分两大类,一类属于普通数据中心,机柜按照XY轴 有序排放,一类属于微模块集合的数据中心,多个机柜组合而成的微模块. 本节课主要详细讲解普通数据中心的可视化展示,浏览器直 ...
- bzoj 4161: Shlw loves matrixI
Description 给定数列 {hn}前k项,其后每一项满足 hn = a1h(n-1) + a2h(n-2) + ... + ak*h(n-k) 其中 a1,a2...ak 为给定数列.请计算 ...