我已经花了3天仔细检查最好的参考文献材料,我可以在互联网上找到有关Q_RETURN_ARG。我已经包括了QQmlComponent。当在C++上使用它发送一个变量在QML上显示时,事情并不总是像它们看起来的那样。也许是因为Qt5是相对较新的,所以我们还没有多少可以依赖的材料。
基本上,代码编译是没有问题的。当我要求它运行时,它将qml页面呈现到设备上,没有问题,然后它得到了错误:
QQmlComponent: Component is not ready
main.cpp:33 (int main(int, char**)): Got QML return: ""除了文件invoke.pro和myapplication.cpp之外,下面是我试图根据这个帖子、Qt5文档、ICS教程、帖子和链接编写的小示例的关键部分
./我的应用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时运行Q_RETURN_ARG,这就是为什么"QQmlComponent组件”没有加载的原因吗?如果我尝试使用“QtQuick2ApplicationViewer查看器”代替"QQmlEngine engine“来代替"QQmlComponent组件”,它会说:“没有调用QQmlComponent的匹配函数”。
有任何建议如何使QQmlComponent初始化,从而使Q_RETURN_ARG开始工作吗?谢谢!
发布于 2014-01-29 10:34:52
我对您的代码进行了快速审查,并使其能够处理一些更改,但它有一些很大(很大!)缺少QT/编码概念。
让它正常工作的一些关键错误:
在这里,您可以查看完整的代码。
http://pastebin.com/PRLBtKWU
我建议您看一看这本书http://qmlbook.org/,并为您当前的QT版本练习QT示例。
https://stackoverflow.com/questions/21010934
复制相似问题