首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Q_RETURN_ARG和QQmlComponent -组件尚未准备好

Q_RETURN_ARG和QQmlComponent -组件尚未准备好
EN

Stack Overflow用户
提问于 2014-01-09 03:05:39
回答 1查看 1.8K关注 0票数 7

我已经花了3天仔细检查最好的参考文献材料,我可以在互联网上找到有关Q_RETURN_ARG。我已经包括了QQmlComponent。当在C++上使用它发送一个变量在QML上显示时,事情并不总是像它们看起来的那样。也许是因为Qt5是相对较新的,所以我们还没有多少可以依赖的材料。

基本上,代码编译是没有问题的。当我要求它运行时,它将qml页面呈现到设备上,没有问题,然后它得到了错误:

代码语言:javascript
复制
QQmlComponent: Component is not ready
main.cpp:33 (int main(int, char**)): Got QML return: ""

除了文件invoke.pro和myapplication.cpp之外,下面是我试图根据这个帖子Qt5文档ICS教程帖子链接编写的小示例的关键部分

./我的应用h

代码语言:javascript
复制
#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

代码语言:javascript
复制
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

代码语言:javascript
复制
#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();
}

这就产生了错误:

代码语言:javascript
复制
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开始工作吗?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-29 10:34:52

我对您的代码进行了快速审查,并使其能够处理一些更改,但它有一些很大(很大!)缺少QT/编码概念。

让它正常工作的一些关键错误:

  • 文件错误只是正确设置文件路径的问题。
  • 加载源需要时间,而不是与其他->一起重新加载未就绪的QQmlEngine的急切序列。(这在我的代码中不是固定的)
  • myQmlFunction需要在QML树中获得更高的位置,或者其他类型的参考帮助。
  • ..。

在这里,您可以查看完整的代码。

http://pastebin.com/PRLBtKWU

我建议您看一看这本书http://qmlbook.org/,并为您当前的QT版本练习QT示例。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21010934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档