我花了3天时间仔细查看我在互联网上找到的关于Q_RETURN_ARG 的最佳参考资料.我已经包含了QQmlComponent.当在C++上使用它来发送变量以在QML上显示时,事情并不像看起来那样.也许是因为Qt5相对较新,我们可以依赖的材料并不多.
基本上,代码编译没有问题.当我要求它运行时,它将qml页面呈现给设备没有问题,然后它得到错误:
QQmlComponent: Component is not ready
main.cpp:33 (int main(int, char**)): Got QML return: ""
Run Code Online (Sandbox Code Playgroud)
除了文件invoke.pro和myapplication.cpp之外,这里是我试图解决的小例子的关键部分,基于这篇文章,Qt5文档,ICS教程,帖子和链接:
./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, …Run Code Online (Sandbox Code Playgroud) 我遇到QML与C++通信的问题.我已经按照预期的所有步骤使示例代码正常运行.在这个小例子工作几个小时后,它归结为一条错误信息,我根本无法摆脱:
./input/main.cpp:18: error:
no matching function for call to
'QObject::connect(QObject*&, const char*, Input*, const char*)'
&input, SLOT(cppSlot(QString)));
^
Run Code Online (Sandbox Code Playgroud)
我读了一些以前关于相关问题的帖子,仔细检查了一切,显然一切看起来都正确.以下是详细信息:
./Sources/main.cpp
#include <QtGui/QGuiApplication>
#include <QtQml/QQmlApplicationEngine>
#include <QtQml/QQmlEngine>
#include <QQuickWindow>
#include <QtDeclarative>
#include <QObject>
#include <QDebug>
#include "Input.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QDeclarativeView view(QUrl::fromLocalFile("input.qml"));
QObject *item = view.rootObject();
Input input;
QObject::connect(item, SIGNAL(qmlSignal(QString)),
&input, SLOT(cppSlot(QString)));
view.show();
return app.exec();
}
Run Code Online (Sandbox Code Playgroud)
./Headers/Input.h
#ifndef INPUT_H
#define INPUT_H
#include <QObject>
#include <QDebug>
class Input : public QObject
{ public:
Input(){} …Run Code Online (Sandbox Code Playgroud) 我已经设置了项目来运行带有插槽通信的C++的QML.现在我偶然发现了这样的信息:
void QDeclarativeView::continueExecute()):
file:///data/data/org.qtproject.example.input/files/Input.qml: File not found
Run Code Online (Sandbox Code Playgroud)
我四处搜索并检查了类似的问题,因此我遵循了这个建议,更改了我的项目文件,如下所示:
QT += qml quick sensors xml
QT += declarative
SOURCES += \
main.cpp
OTHER_FILES += \
Input.qml
HEADERS += \
Input.h
RESOURCES += \
Input.qrc
# Required for deployment (?) :
QML_FILES.source = qml
QML_FILES.target = .
DEPLOYMENTFOLDERS += QML_FILES
# Required for deployment too (?) :
include(qmlapplicationviewer/qmlapplicationviewer.pri)
qtcAddDeployment()
Run Code Online (Sandbox Code Playgroud)
虽然将这个hack添加到项目文件似乎适用于Symbian和Harmattan,但对于Android,最后两行不能解决问题,因为编译器会给出消息:
./input/qmlapplicationviewer/qmlapplicationviewer.pri:-1: error: No such file or directory
./input.pro:22: 'qtcAddDeployment' is not a recognized test function.
Run Code Online (Sandbox Code Playgroud)
即使对于Qt5,我们是否应该担心目标,即使我们定义项目时我们已经为Android设备设置了它?
那么,有人可以就如何将QML部署到Android设备提出任何建议吗?也许一些相同的黑客会做同样的魔术,适用于Symbian,但也适用于Android?