我花了3天的时间仔细检查我可以在网上找到的有关Q_RETURN_ARG的最佳reference Material 。我已经包含了QQmlComponent。在C++上使用它发送变量以在QML上显示时,情况并不总是像看起来那样。也许是因为Qt5相对较新,所以我们可以依靠的 Material 不多。
基本上,代码可以毫无问题地进行编译。当我要求它运行时,它将qml页面毫无问题地呈现到设备上,然后出现错误:
QQmlComponent: Component is not ready
main.cpp:33 (int main(int, char**)): Got QML return: ""
除了文件invoke.pro和myapplication.cpp外,这是我要尝试的小示例的关键部分,基于post,Qt5 documentation,ICS tutorial,post和link:
./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();
}
这给出了错误:
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: ""
我注意到main.qml是要使用“QtQuick2ApplicationViewer”而不是“QQmlEngine”在Android上加载的。在与Android打交道时,是否应该完全不使用“QQmlEngine”来加载main.qml来尝试使Q_RETURN_ARG运行,因此这就是为什么不加载“QQmlComponent组件”的原因?如果我尝试使用“QtQuick2ApplicationViewer查看器”将“QQmlComponent组件”替换为“QQmlEngine引擎”,则会显示:“没有匹配功能可用于调用QQmlComponent”。
关于如何初始化QQmlComponent的任何建议,以便Q_RETURN_ARG开始工作?谢谢!
最佳答案
我快速回顾了您的代码,并对其进行了一些更改,但是它缺少一些重要的QT/编码概念。
使它工作的一些关键错误:
在这里,您可以查看完整的代码。
http://pastebin.com/PRLBtKWU
我建议您阅读本书http://qmlbook.org/,并使用当前QT版本的QT示例进行练习。
关于android - Q_RETURN_ARG和QQmlComponent-组件未准备好,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21010934/