• 6044阅读
  • 6回复

[提问]QML采用动态方式创建对象遇到复杂类型属性提示Component not ready [复制链接]

上一主题 下一主题
离线彩阳
 

只看楼主 倒序阅读 楼主  发表于: 2014-04-29
问题是这样的,我想用动态创建的方式在Javascript函数中创建QML对象。这是一个简单的例子:
// Block.qml
Item
{
    id: block
    property list<url> imageSources;
    ……
}

当我使用:
var Block = Qt.createComponent( "Block.qml" );
var block = Block.createObject( parent );
的时候,执行提示QQmlComponent: Component is not ready。
可是去掉了list<url> imageSources这样的属性后就能够顺利地创建Component。我怀疑是因为imageSources是复杂类型(引用类型)导致的。采用了其它的object类型作为属性也会出现同样的问题。
想知道大家遇到了这样的情况究竟是如何解决的。
上海Qt开发联盟,热忱地欢迎你的加入!
离线toby520

只看该作者 1楼 发表于: 2014-04-29
呵呵,qml写的太复杂了,我好像写了这么久还没有用到过这么复杂的类型
QtQML多多指教开发社区 http://qtclub.heilqt.com
将QtCoding进行到底
关注移动互联网,关注金融
开发跨平台客户端,服务于金融行业
专业定制界面
群号:312125701   373955953(qml控件定做)
离线wayne

只看该作者 2楼 发表于: 2014-04-29
用javascript语法类型吧:
  1. property var theArray: new Array()

离线彩阳

只看该作者 3楼 发表于: 2014-04-29
回 wayne 的帖子
wayne:
用javascript语法类型吧:
  1. property var theArray: new Array()



不行吧。QML里面的属性都是静态的,怎能使用javascript的语法呢?
下面是错误截图:
上海Qt开发联盟,热忱地欢迎你的加入!
离线彩阳

只看该作者 4楼 发表于: 2014-04-29
上海Qt开发联盟,热忱地欢迎你的加入!
离线wayne

只看该作者 5楼 发表于: 2014-04-29
  1. Item {
  2.     property var theArray: new Array()
  3.     property var theDate: new Date()
  4.     Component.onCompleted: {
  5.         for (var i = 0; i < 10; i++)
  6.             theArray.push("Item " + i)
  7.         console.log("There are", theArray.length, "items in the array")
  8.         console.log("The time is", theDate.toUTCString())
  9.     }
  10. }


离线彩阳

只看该作者 6楼 发表于: 2014-04-29
回 wayne 的帖子
wayne:Item {
    property var theArray: new Array()
    property var theDate: new Date()
    Component.onCompleted: {
....... (2014-04-29 19:15) 

你用javascript的这两个函数试试:
var Block = Qt.createComponent( "Block.qml" );
var block = Block.createObject( parent );
我是一直都是QQmlComponent: Component is not ready这个问题。
上海Qt开发联盟,热忱地欢迎你的加入!
快速回复
限100 字节
 
上一个 下一个