有两种方法可以创建,都是全局对象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. 性能测试工具Jmeter12-Jmeter连接配置带跳板机(SSH)的mysql服务器

    准备工作: 1.下载mysql-connector-java-5.1.13-bin.jar包,然后放在jmeter安装目录lib下 2.重新打开jmeter即可 要完成以上两步才能进行后续操作 要点: ...

  2. Python openpyxl Read

    #! /usr/bin/env python # coding=utf-8 from openpyxl import Workbook, load_workbook wb = load_workboo ...

  3. Full Text Search 实现Sort的实现方案

    CREATE TABLE dbo.pageStore( ID int NOT NULL, StoreName varchar(50) NULL, OwnerOccupation varchar(50) ...

  4. js需要清楚的内存模型

    原型 原型重写 原型继承 组合方式实现继承 函数作用域链

  5. git 学习之什么是版本库

    什么是版本库? 我们一般把版本库也叫仓库(repository),其实我们可以简单的把它看成一个目录,只不过目录里面的文件都会由 Git 进行管理,当我们对文件进行修改.删除.Git 都可以对其进行跟 ...

  6. poj 2572 Hard to Believe, but True!

    Hard to Believe, but True! Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3537   Accep ...

  7. C# [method Modifiers] abstract virtual override new

    abstract :表示方法是抽象方法,在子类中必须重写.抽象方法所在的类必须是抽象类,即用abstract modifiers:virtual:表示此方法是virtual方法,除了在子类中可以重写外 ...

  8. [转]微信小程序联盟 跳坑《一百八十一》设置API:wx.openSetting使用说明

    本文转自:http://www.wxapp-union.com/forum.php?mod=viewthread&tid=4066 这个API解决了过去一个长久以来无法解决的问题,如何让用户重 ...

  9. SQL SEVERE 基本用法 1

    知识点: 数据库的存储结构分为逻辑存储结构和物理存储结构两种, 其中逻辑存储结构指是由那些信息组成,物理存储结构是指如何在磁盘上存储数据库文件的. 数据库文件由数据文件和事务日志文件组成,一个数据库至 ...

  10. thinkphp下判断状态值语法

    在thinkphp框架下我们经常会用到状态值的判断:但是这样写会引起语法错误. <div> <if condition="{$res.status} eq '0'" ...