我想从使用Qt.createQmlObject()函数中example但第一次获得该错误的QML的字符串创建一个QML项目:“错误:Qt.createQmlObject():组件是没有准备好”,在正确创建第二次项目, 怎么了?
你可以看到 - 我是尝试各种项目:项目,矩形,组件(只有一个有“身份”属性)
测试应用程序是:main.cpp中:
#include <QApplication>
#include <QWSServer>
#include <QDeclarativeView>
int main(int argc, char *argv[])
{
QApplication a(argc, argv, QApplication::GuiServer);
QDeclarativeView view;
view.setMinimumSize(100,100);
view.setResizeMode(QDeclarativeView::SizeRootObjectToView);
view.show();
view.setSource(QUrl::fromUserInput("qrc:/createFromStringTest.qml"));
return a.exec();
}
createFromStringTest.qml:
import QtQuick 1.1
Rectangle {
id: rootRectangle
objectName: "rootRectangle"
anchors.centerIn: parent
anchors.fill: parent
color: "gray"
border.width: 5
border.color: "black"
width: 50
height: 50
property int testCount: 0
MouseArea {
anchors.fill: parent
onClicked: {
testCount +=1;
console.log("====================== Runing test "+testCount+" ======================");
tests()
}
}
// what is right?
Item{
id: parentItem
objectName: "parentItem"
Component.onCompleted: {
console.log("parentItem loaded");
}
}
Component {
id: parentComponent
Item {
id: parentComponentItem
Component.onCompleted: {
console.log("parentComponentItem loaded");
}
}
}
property list<Item> parentListItem
property list<Component> parentListComponent
Rectangle {
id: parentRectangle
objectName: "parentRectangle"
Component.onCompleted: {
console.log("parentRectangle loaded");
}
}
Component.onCompleted: {
console.log("rootRectangle loaded ");
}
Component.onDestruction: {
console.log("rootRectangle destroyed ");
}
function tests(){
try{
var newObjectparentItem = Qt.createQmlObject('import QtQuick 1.1; Rectangle {objectName: "dynparentItem";anchors.centerIn: parent; anchors.fill: parent; border.width: 10; border.color: "red";}',parentItem,"parentItem:");
console.log("parentItem OK ");
}catch(e){
console.log("parentItem error: "+e);
}
try{
var newObjectparentComponent = Qt.createQmlObject('import QtQuick 1.1; Rectangle {objectName: "dynparentComponent";anchors.centerIn: parent; anchors.fill: parent; border.width: 10; border.color: "red";}',parentComponent,"parentComponent:");
console.log("parentComponent OK ");
}catch(e){
console.log("parentComponent error: "+e);
}
try{
var newObjectparentComponentItem = Qt.createQmlObject('import QtQuick 1.1; Rectangle {objectName: "dynparentComponentItem";anchors.centerIn: parent; anchors.fill: parent; border.width: 10; border.color: "red";}',parentComponentItem,"parentComponentItem:");
console.log("parentComponentItem OK ");
}catch(e){
console.log("parentComponentItem error: "+e);
}
try{
var newObjectparentListItem = Qt.createQmlObject('import QtQuick 1.1; Rectangle {objectName: "dynparentListItem";anchors.centerIn: parent; anchors.fill: parent; border.width: 10; border.color: "red";}',parentListItem,"parentListItem:");
console.log("parentListItem OK ");
}catch(e){
console.log("parentListItem error: "+e);
}
try{
var newObjectparentListComponent = Qt.createQmlObject('import QtQuick 1.1; Rectangle {objectName: "dynparentListComponent";anchors.centerIn: parent; anchors.fill: parent; border.width: 10; border.color: "red";}',parentListComponent,"parentListComponent:");
console.log("parentListComponent OK ");
}catch(e){
console.log("parentListComponent error: "+e);
}
try{
var newObjectparentRectangle = Qt.createQmlObject('import QtQuick 1.1; Rectangle {objectName: "dynparentRectangle";anchors.centerIn: parent; anchors.fill: parent; border.width: 10; border.color: "red";}',parentRectangle,"parentRectangle:");
console.log("parentRectangle OK ");
}catch(e){
console.log("parentRectangle error: "+e);
}
}
}
输出:
Qml debugging is enabled. Only use this in a safe environment!
rootRectangle loaded
parentRectangle loaded
parentItem loaded
====================== Runing test 1 ======================
parentItem error: Error: Qt.createQmlObject(): Component is not ready
parentComponent error: Error: Qt.createQmlObject(): Component is not ready
parentComponentItem error: ReferenceError: Can't find variable: parentComponentItem
parentListItem error: Error: Qt.createQmlObject(): Missing parent object
parentListComponent error: Error: Qt.createQmlObject(): Missing parent object
parentRectangle error: Error: Qt.createQmlObject(): Component is not ready
====================== Runing test 2 ======================
parentItem OK
parentComponent OK
parentComponentItem error: ReferenceError: Can't find variable: parentComponentItem
parentListItem error: Error: Qt.createQmlObject(): Missing parent object
parentListComponent error: Error: Qt.createQmlObject(): Missing parent object
parentRectangle OK
rootRectangle destroyed
二手的Qt 4.8
对于情况下,像你使用Qt.createComponent动态初始化您使用的组件。随着组件初始化,你可以打电话,然后Component.CreateObject。或者你也可以使用的一个为父级:parentComponent.CreateObject(QmlItem, "QML properties")
。我通常使用Qt.createQmlObject对该QML文件立即做动态的东西与QML窗口,并没有为它创建任何新的QML上下文。
记住,在大多数情况下,你甚至不需要使用既不以上,但操纵一些QML Repeater并为它的数据模型。这样,你可以提供智能的委托定制动态创建的项目。但这个话题是不是回答你的问题更多。
从我的经验,我得到了“组件没有准备好”的错误时,动态地创建与Qt.createComponent + Component.createObject对象,并没有在分量的QML错误。
找到这个错误的方法之一是该组件的一个实例静态地添加到您的QML的应用程序,使之与行号错误将会运行应用程序时进行打印。
在功能Qt.createQmlObject(QML字符串,父ID,文件名)在我的例子中,文件名的第三个参数错误包括在“:”符号 - 没有它按预期工作!