Why does the error "QDeclarativeComponent: Component is not ready" occur?

I did a few things, but got stuck in one specific example. The code is similar to

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; 

Simple, but when I run this code in my Qt (5.0), it shows something like QDeclarativeComponent: the component is not ready.

I know something is missing. In google, I found that the method should be declared as Q_INVOKABLE, but I do not understand why?

0
c ++ qt qml
source share
1 answer

When you create a component in QML, the first step is to parse the QML file. This happens when you call:

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

Then, before any call to QDeclarativeComponent::create , you need to wait for the component status to go to Ready . You can track state changes by processing statusChanged .

Create an instance of the component when the component is ready.

0
source share

All Articles