Error: Qt.createQmlObject(): Component is not ready

Error: Qt.createQmlObject(): Component is not ready

我想使用 Qt.createQmlObject() 函数从 QML 字符串创建一个 QML 项目,就像在 example 中一样,但第一次得到错误:"Error: Qt.createQmlObject(): Component is not ready",在第二次项目中正确创建,有什么问题吗?

你可以看到 - 我正在尝试各种项目:Item、Rectangle、Component(只有一个有 "status" 属性)

测试应用程序是: 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

对于像您这样的情况,请立即在此 QML 文件中使用 Qt.createComponent to dynamically initialize the component you use. With component initialized you can call then Component.CreateObject. Or you could have used one parentComponent for that: parentComponent.CreateObject(QmlItem, "QML properties"). I usually use Qt.createQmlObject 使用 QML window 执行动态操作,不要为其创建任何新的 QML 上下文。

请注意,在大多数情况下,您甚至不需要使用上述任何一种方法,只需操作一些 QML Repeater 及其数据模型即可。这样您就可以提供智能委托来自定义动态创建的项目。但是这个话题不仅仅是对你问题的回答。

函数的第三个参数出错 Qt.createQmlObject(QML 字符串,父 ID,文件名)我的示例中的文件名包含“:”符号 - 没有它按预期工作!

根据我的经验,我在使用 Qt.createComponent + Component.createObject 动态创建对象时遇到 "Component is not ready" 错误,并且组件中存在 QML 错误。

发现错误的一种方法是将该组件的实例静态添加到您的 QML 应用程序,以便在 运行 应用程序时打印带有行号的错误。