我花了3天的时间仔细检查我在互联网上找到的关于Q_RETURN_ARG的最佳reference材料。我已经加入了QQmlComponent。当在C ++上使用它来发送变量以在QML上显示时,事情并不像看起来那样。也许是因为Qt5相对较新,我们可以依赖的材料并不多。
基本上,代码编译没有问题。当我要求它运行时,它将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上加载。是不应该使用“QQmlEngine”来加载main.qml以试图让Q_RETURN_ARG运行 - 在处理Android时,这就是为什么“QQmlComponent组件”没有加载?如果我尝试使用“QtQuick2ApplicationViewer viewer”替换“QQmlEngine engine”作为“QQmlComponent组件”,它会说:“没有用于调用QQmlComponent的匹配函数”。
有关如何初始化QQmlComponent的任何建议,那么Q_RETURN_ARG是否开始工作?谢谢!
答案 0 :(得分:7)
我快速查看了您的代码,并对其进行了一些更改,但它缺少一些大的(BIG!)QT /编码概念。
使其运作的一些关键错误:
在这里,您可以查看完整代码。
我建议您查看本书http://qmlbook.org/并练习当前QT版本的QT示例。