有两种方法可以创建,都是全局对象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对象的更多相关文章

  1. javascript创建自定义对象和prototype

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. JavaScript 创建类/对象的几种方式

    在JS中,创建对象(Create Object)并不完全是我们时常说的创建类对象,JS中的对象强调的是一种复合类型,JS中创建对象及对对象的访问是极其灵活的. JS对象是一种复合类型,它允许你通过变量 ...

  3. JavaScript—创建正则对象

    创建正则对象 方式1: var reg = new RegExp('\d', 'i');var reg = new RegExp('\d', 'gi'); 方式2: var reg = /\d/i;v ...

  4. JavaScript创建Map对象(转)

    JavaScript 里面本身没有map对象,用JavaScript的Array来实现Map的数据结构. /* * MAP对象,实现MAP功能 * * 接口: * size()     获取MAP元素 ...

  5. 【微信小程序开发之坑】javascript创建date对象

    最近开发中用到date,开始以如下方式来创建: var date = new Date('2018-01-30 11:00:00'); 在开发工具上,调试,ios 和 android都好好的. 在真机 ...

  6. Qt Quick 组件和动态创建的对象具体的解释

    在<Qt Quick 事件处理之信号与槽>一文中介绍自己定义信号时,举了一个简单的样例.定义了一个颜色选择组件,当用户在组建内点击鼠标时,该组件会发出一个携带颜色值的信号,当时我使用 Co ...

  7. Ajax 学习之创建XMLHttpRequest对象------Ajax的核心

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  8. JavaScript之ECMA对象的学习

    从传统意义上来说,ECMAScript 并不真正具有类.事实上,除了说明不存在类,在 ECMA-262 中根本没有出现“类”这个词.ECMAScript 定义了“对象定义”,逻辑上等价于其他程序设计语 ...

  9. js如何创建JSON对象

    js如何创建JSON对象 一.总结 一句话总结:直接创建js数组和js对象即可,然后JSON.stringify就可以获取json字符串,js中的一切都是对象,而且js中的对象都是json对象 js ...

随机推荐

  1. qt下qmake:提示could not exec '/usr/lib/x86_64-linux-gnu/qt4/bin/qmake': No such file or directory

    编译出现的问题解决方法: 打开终端输入,qmake -v,出现错误:qmake: could not exec '/usr/lib/x86_64-linux-gnu/qt4/bin/qmake': N ...

  2. 卸载Windows,安装纯Linux

    操作步骤 下载安装一键GHOST优盘版 参考帮助文档,安装U盘启动 重启电脑,F12进入模式选择界面,选择USB device 选择DOS 工具 进入Disk Genius 删除掉除主分区(0)外的其 ...

  3. python 流式游标读取mysql大型数据库

    import asyncio import aiomysql async def dbdaochu(loop): sqlstr='sql' conn = await aiomysql.connect( ...

  4. SQL Server触发器创建、删除、修改、查看

    一:触发器是一种特殊的存储过程,它不能被显式地调用,而是在往表中插入记录﹑更新记录或者删除记录时被自动地激活.所以触发器可以用来实现对表实施复杂的完整性约束. 二:SQL Server为每个触发器都创 ...

  5. 案例20-页面使用redis缓存显示类别菜单

    1 准备工作 1  需要导入所需要的jar包. 2 启动windows版本的redis服务端 3 准备JedisUtils工具类的配置文件redis.properties redis.maxIdle= ...

  6. FocusBI: SQL Server内核

    关注微信公众号:FocusBI 查看更多文章:加QQ群:808774277 获取学习资料和一起探讨问题. <商业智能教程>pdf下载地址 链接:https://pan.baidu.com/ ...

  7. ComboBox ItemHeight 再高一点

    public static void BindData(this ComboBox box, List<KeyValuePair<long, string>> data) { ...

  8. nodejs图片裁剪、水印(使用images)

    /** * Created by chaozhou on 2015/9/21. */ var images = require("images"); /** * 缩放图像 * @p ...

  9. ssh登录实现

    工程目录 配置文件详解 Spring的applicationContext.xml文件 <span ><?xml version="1.0" encoding=& ...

  10. CSS 文字超长省略显示并隐藏超长部分

    1.包含文字的元素必须是块级元素,不是块级元素使用display:block使其具有块级元素属性: 2.具备上述基本条件后,css样式如下: { display: block; max-width: ...