Details
-
Bug
-
Resolution: Incomplete
-
Not Evaluated
-
None
-
Qt Creator 3.0.0
-
None
-
Crosscompiling: host linux ubuntu, target Android.
Description
I have spend 3 days carefully checking the best material available (see reference) material I could find on internet about Q_RETURN_ARG. I have already included QQmlComponent. When using it on C++ to send a variable to display on QML, things are not always as they seems. Maybe because Qt5 is relatively new, there is not much material put together that we can rely on yet.
Basically, the code compiles without problem. When I ask it to run, it renders the qml page to the device with no problem, and then it gets the error:
QQmlComponent: Component is not ready
main.cpp:33 (int main(int, char**)): Got QML return: ""
Besides the files invoke.pro and myapplication.cpp, here are the key parts of the small example I am trying to work out, based on references that you can see in the end.
./myapplication.h
#include <QObject> #include <QDebug> #include <QQmlComponent> class MyApplication : public QObject { Q_OBJECT public: explicit MyApplication(QObject *parent = 0); ~MyApplication(void) {} QObject *object; QQmlComponent *component; void loadComponent(void) { QQmlEngine engine; QQmlComponent *component = new QQmlComponent(&engine); component->loadUrl(QStringLiteral("qml/invoke/main.qml")); if(!component->isReady()){ qWarning("qPrintable: %s", qPrintable(component->errorString())); } if (component->isLoading()){ cout <<"==== component->isLoading ===="; QObject::connect(component, SIGNAL(statusChanged()), this, SLOT(continueLoading())); } else{ cout <<"==== component is not Loading ===="; continueLoading(); } } signals: public slots: void continueLoading() { QQmlEngine engine; QQmlComponent *component = new QQmlComponent(&engine); component->loadUrl(QStringLiteral("qml/invoke/main.qml")); if (component->isError()) { qWarning() << "component->isError()="<< component->errors(); } else { object = component->create(); cout <<"object created"; } } };
./main.qml
import QtQuick 2.0 Rectangle { width: 360 height: 360 Item { function myQmlFunction(msg_cpp) { console.log("Got msg_cpp:", msg_cpp) return "output" } } }
./main.cpp
#include <QtGui/QGuiApplication> #include "qtquick2applicationviewer.h" #include <QtQuick/QQuickItem> #include <QtQuick/QQuickView> #include <QQmlEngine> #include <QtQml> #include <QDeclarativeEngine> #include <QtCore> #include <QtQuick/QtQuick> #include <QQmlComponent> #include <QtQml/qqml.h> #include "myapplication.h" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QtQuick2ApplicationViewer viewer; viewer.setMainQmlFile(QStringLiteral("qml/invoke/main.qml")); MyApplication *myClass = new MyApplication(); myClass->loadComponent(); QObject *object=myClass->object; QVariant returnedValue; QVariant msg_cpp = "C++ message"; QMetaObject::invokeMethod(object, "myQmlFunction", Q_RETURN_ARG(QVariant, returnedValue), Q_ARG(QVariant, msg_cpp)); qDebug() << "Got QML return:" << returnedValue.toString(); viewer.showExpanded(); delete object; return app.exec(); }
Which gives the error:
loadComponent()): qPrintable: file://qml/invoke/main.qml:-1 File not found continueLoading()): component->isError()= (file://qml/invoke/main.qml: File not found) main.cpp:36 (int main(int, char**)): Got QML return: ""
I noticed that main.qml is meant to load on Android using "QtQuick2ApplicationViewer" instead of "QQmlEngine". Should the "QQmlEngine" not be used at all for loading main.qml in attempt to get Q_RETURN_ARG running - when dealing with Android, and therefore that is why "QQmlComponent component" is not loading? If I try to use " QtQuick2ApplicationViewer viewer" replacing "QQmlEngine engine" for "QQmlComponent component", it says: "no matchning function for call to QQmlComponent".
Any suggestions how to get QQmlComponent initialized, so Q_RETURN_ARG starts working? Thanks!
References:
[1] http://qt.developpez.com/doc/4.7-snapshot/qtbinding/l
[2] http://stackoverflow.com/questions/19271200/error-variable-qqmlcomponent-component-has-initializer-but-incomplete-type-in
[3] http://stackoverflow.com/questions/20653369/why-do-i-get-a-qdeclarativecomponentcomponent-is-not-ready-error
[4] http://qt-project.org/doc/qt-5.0/qtqml/qqmlcomponent.html
[5] http://www.ics.com/blog/integrating-c-qml#.Us6Qr_ht14p
[6] http://stackoverflow.com/questions/7180162/qt-no-matching-function-for-call-to-mainwindowconnect
[7] http://stackoverflow.com/questions/1935147/argument-type-for-qt-signal-and-slot-does-const-reference-qualifiers-matters