Why do I get a “QDeclarativeComponent:Component is not ready” error?

痞子三分冷 提交于 2019-12-13 21:22:15

问题


I have done few things but stuck in one particular example. The code is like

MyItem.qml

 import QtQuick 1.0

 Item {
     function myQmlFunction(msg) {
         console.log("Got message:", msg)
         return "some return value"
     }
 }

main.cpp

 QDeclarativeEngine engine;
 QDeclarativeComponent component(&engine, "MyItem.qml");
 QObject *object = component.create();

 QVariant returnedValue;
 QVariant msg = "Hello from C++";
 QMetaObject::invokeMethod(object, "myQmlFunction",
         Q_RETURN_ARG(QVariant, returnedValue),
         Q_ARG(QVariant, msg));

 qDebug() << "QML function returned:" << returnedValue.toString();
 delete object;

A Simple one but when i run this code in my qt(5.0) it shows something like QDeclarativeComponent:Component is not ready.

I know I'm missing something. On google i found that the method should be declared as Q_INVOKABLE but i don't get it why?


回答1:


When you create a component in QML, the first step is to parse the QML file. This is what happen when you are invoking :

QDeclarativeComponent component(&engine, "MyItem.qml");

Then, prior any call on QDeclarativeComponent::create, you have to wait that the component status pass to Ready. You can track status changes by handling statusChanged signal.

Create your component instance once component becomes ready.



来源:https://stackoverflow.com/questions/20653369/why-do-i-get-a-qdeclarativecomponentcomponent-is-not-ready-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!