Signals 如何调用c++;来自qml的函数,并更改标签文本

Signals 如何调用c++;来自qml的函数,并更改标签文本,signals,qml,blackberry-10,blackberry-cascades,slots,Signals,Qml,Blackberry 10,Blackberry Cascades,Slots,我是黑莓10开发的新手。我创建了一个简单的BB 10 cascades项目。 我想通过C++函数来修改标签的文本。 main.qml import bb.cascades 1.0 Page { content: Container { id: containerID Button { id: button1 objectName: "button"

我是黑莓10开发的新手。我创建了一个简单的BB 10 cascades项目。 我想通过C++函数来修改标签的文本。 main.qml

      import bb.cascades 1.0    
      Page {
         content: Container {
         id: containerID
         Button {
            id: button1
            objectName: "button"
            text: "text"
            onClicked: {
                btnClicked("New Label Text");
            }
        }
        Label {
            id: label1
            objectName: "label1"
            text: "Old Label Text"
        }
    }
}
现在我要在哪个文件中声明,在哪个文件中定义函数btnClicked(QString)函数

HelloBB.hpp

// Default empty project template
#ifndef HelloBB_HPP_
#define HelloBB_HPP_

#include <QObject>

namespace bb { namespace cascades { class Application; }}

class HelloBB : public QObject
{
    Q_OBJECT
    public:
    HelloBB(bb::cascades::Application *app);

    virtual ~HelloBB() {}

};

#endif
// Default empty project template
#include "HelloBB.hpp"
#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>

using namespace bb::cascades;
HelloBB::HelloBB(bb::cascades::Application *app) : QObject(app)
{
    // create scene document from main.qml asset
    //set parent to created document to ensure it exists for the whole application lifetime
    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

    qml->setContextProperty("app", this);

    // create root object for the UI
    AbstractPane *root = qml->createRootObject<AbstractPane>();

    // set created root object as a scene
    app->setScene(root);
}
//默认的空项目模板
#ifndef HelloBB_水电站_
#定义HelloBB_水电站_
#包括
命名空间bb{命名空间级联{类应用程序;}}
类HelloBB:公共QObject
{
Q_对象
公众:
HelloBB(bb::cascades::Application*app);
虚拟~HelloBB(){}
};
#恩迪夫
HelloBB.cpp

// Default empty project template
#ifndef HelloBB_HPP_
#define HelloBB_HPP_

#include <QObject>

namespace bb { namespace cascades { class Application; }}

class HelloBB : public QObject
{
    Q_OBJECT
    public:
    HelloBB(bb::cascades::Application *app);

    virtual ~HelloBB() {}

};

#endif
// Default empty project template
#include "HelloBB.hpp"
#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>

using namespace bb::cascades;
HelloBB::HelloBB(bb::cascades::Application *app) : QObject(app)
{
    // create scene document from main.qml asset
    //set parent to created document to ensure it exists for the whole application lifetime
    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

    qml->setContextProperty("app", this);

    // create root object for the UI
    AbstractPane *root = qml->createRootObject<AbstractPane>();

    // set created root object as a scene
    app->setScene(root);
}
//默认的空项目模板
#包括“HelloBB.hpp”
#包括
#包括
#包括
使用名称空间bb::cascades;
HelloBB::HelloBB(bb::cascades::Application*app):QObject(app)
{
//从main.qml资源创建场景文档
//将父级设置为created document,以确保它在整个应用程序生命周期内都存在
QmlDocument*qml=QmlDocument::create(“asset:///main.qml)母公司(本公司);
qml->setContextProperty(“应用程序”,此);
//为UI创建根对象
抽象窗格*root=qml->createRootObject();
//将创建的根对象设置为场景
应用->设置场景(根);
}
现在我想将标签文本从旧标签文本更改为用户给定文本。我从QML调用C++函数。我不知道在哪里定义这个函数,以及如何将这个C++函数与QML连接。

谢谢。

< p>这里可以找到C++和QML集成的文档:

正如克利夫所说:

在HelloBB构造函数中,您可以向QML公开该类,如下所示:

    qml->setContextProperty("HelloBB", this);

然后在C++中创建一个方法,您可以从QML调用。请记住,该方法必须标记为Q_INVOKABLE才能从QML调用

考虑一下这一点:

在HelloBB.hpp中:

    public:
           Q_INVOKABLE void test();
在HelloBB.cpp中:

    void HelloBB::test() {
        qDebug() << "TEST";
    }

通过C++查找标签,使用CA:

Label*yourL=root->findChild(LabelObjName);
yourL->SetText(“我的新漂亮文本”);

请务必添加:

#包括

并在类中使用root作为私有变量,以便也可以在其他方法中访问对象

bb::cascades::AbstractPane*root;

问候