2013-07-26 100 views
0

我是黑莓10开发的新手。我已经创建了简单的BB 10级联项目。 我想通过C++函数更改标签的文本。如何从qml调用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 

HelloBB.cpp

// 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); 
} 

现在我想从旧标签文本标签文本更改为用户给定文本。我从qml调用C++函数。我不知道在哪里定义这个函数,以及如何从qml连接这个C++函数。

谢谢。

回答

2

你可以找到整合C++和QML这里的文档:http://developer.blackberry.com/cascades/documentation/dev/integrating_cpp_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"; 
    } 

       主要。QML:

onClicked: { 
     HelloBB.test() 
    } 
0

发现通过C标签++你CA使用:

Label* yourL = root->findChild<Label*>(LabelObjName); yourL->SetText("my new beautiful text);

一定要加:

#include <bb/cascades/Button>

,并使用root作为私有变量您所以你可以通过其他方法访问对象

bb::cascades::AbstractPane *root;

关于