2013-07-26 10 views
0

Blackberry 10 개발을 처음 사용했습니다. 저는 간단한 BB 10 계단식 프로젝트를 만들었습니다. C++ 함수를 통해 레이블의 텍스트를 변경하려고합니다.qml에서 C++ 함수를 호출하고 lable 텍스트를 변경하는 방법

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++ 함수를 연결하는 방법을 알지 못합니다.

감사합니다.

당신은 C++ 여기 QML 통합 문서를 찾을 수 있습니다

답변

2

: 당신이 그렇게 같이 QML에 클래스를 노출 할 수 HelloBB 생성자에서

:

qml->setContextProperty("HelloBB", this); 
절벽의 노트로 http://developer.blackberry.com/cascades/documentation/dev/integrating_cpp_qml/

그리고 QML에서 호출 할 수있는 C++에서 메서드를 만듭니다. QML에서 호출하려면 메서드를 Q_INVOKABLE로 표시해야합니다.

이를 고려 HelloBB.hpp에서

        : HelloBB.cpp에서

public: 
      Q_INVOKABLE void test(); 

        :

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>

사용 루트를 민간 변수로 클래스이므로 다른 메소드에서도 객체에 액세스 할 수 있습니다.

안부