2012-09-18 42 views
4

我工作的一個QML應用程序(黑莓10)訪問QML對象,並有一個QML文件是這樣的:不能在C++代碼

import bb.cascades 1.0  
Page { 
     content: Container { 
      id: containerID 
      Button { 
       id: button1 
       text: "text" 
       onClicked: { 
       } 
      } 
      Label { 
       id: label1 
       text: "text" 
      } 
     } 
    } 

現在我想訪問label1在我的C++代碼,所以我有以下代碼:

#include "app.hpp" 

#include <bb/cascades/Application> 
#include <bb/cascades/QmlDocument> 
#include <bb/cascades/AbstractPane> 

using namespace bb::cascades; 

App::App() 
{ 
    QmlDocument *qml = QmlDocument::create("main.qml"); 
    //-- setContextProperty expose C++ object in QML as an variable 
    //-- uncomment next line to introduce 'this' object to QML name space as an 'app' variable 
    //qml->setContextProperty("app", this); 

    AbstractPane *root = qml->createRootNode<AbstractPane>(); 

    QObject *labelTest = root->findChild<QObject*>("label1"); 
    if (labelTest) 
     labelTest->setProperty("text", "yes!!"); 

    Application::setScene(root); 
} 

現在我運行該應用程序,但標籤的文本不會更改。

有什麼不對?

回答

5

我自己找到了答案。 的QML代碼:

import bb.cascades 1.0 

//-- create one page with a label and text 

Page { 
    property alias labelText: label.text  
    content: Container {    
     Label { 
      id: label 
      text: "Label" 
     } 

     Button { 
      objectName: "button" 
      text: "Button" 
     }        
    } 
} 

和C++代碼:

#include "app.hpp" 

#include <bb/cascades/Application> 
#include <bb/cascades/QmlDocument> 
#include <bb/cascades/AbstractPane> 

using namespace bb::cascades; 

App::App() 
{ 
    QmlDocument *qml = QmlDocument::create("main.qml"); 
    //-- setContextProperty expose C++ object in QML as an variable 
    //-- uncomment next line to introduce 'this' object to QML name space as an 'app' variable 
    //qml->setContextProperty("app", this); 

    AbstractPane *root = qml->createRootNode<AbstractPane>(); 

    root->setProperty("labelText", "yes"); 

    QObject *newButton = root->findChild<QObject*>("button"); 
    if (newButton) 
     newButton->setProperty("text", "New button text"); 

    Application::setScene(root); 
} 
+0

+1救了我很多 –

0

一種替代的解決方案是...

QML:

import bb.cascades 1.0 

//-- create one page with a label and text 

Page { 
    property alias labelText: label.text  
    content: Container {    
     Label { 
      id: label 
      text: "Label" 
      objectName: "label1" 
     } 

     Button { 
      objectName: "button" 
      text: "Button" 
     }        
    } 
} 

C++

#include "app.hpp" 

#include <bb/cascades/Application> 
#include <bb/cascades/QmlDocument> 
#include <bb/cascades/AbstractPane> 

using namespace bb::cascades; 

App::App() 
{ 
    QmlDocument *qml = QmlDocument::create("main.qml"); 
    //-- setContextProperty expose C++ object in QML as an variable 
    //-- uncomment next line to introduce 'this' object to QML name space as an 'app' variable 
    //qml->setContextProperty("app", this); 

    AbstractPane *root = qml->createRootNode<AbstractPane>(); 

    QObject *labelTest = root->findChild<QObject*>("label1"); 
    if (labelTest) 
     labelTest->setText(QString("yes!!")); 

    Application::setScene(root); 
} 

但是您提供的解決方案在很多方面都更好。

+0

我想labelTest會被鑄造成Label,我錯了嗎?或者你可以只聲明它:Label * labelTest = root-> findChild (「label1」); –