2012-10-05 89 views
2

我想調用C++函數從級聯QML調用C++從級聯QML功能

這是CalcolatorQML.cpp

// Default empty project template 
#include "CalcolatorQML.hpp" 

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

using namespace bb::cascades; 

CalcolatorQML::CalcolatorQML(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); 

// create root object for the UI 
AbstractPane *root = qml->createRootObject<AbstractPane>(); 
// set created root object as a scene 
app->setScene(root); 
//Container *container = root->findChild<Container*>("myContainer"); 
} 
void CalcolatorQML::injectContainer(){ 
qDebug("NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN"); 
} 

,我聲明功能Q_INVOKABLE void injectContainer(); 這是CalcolatorQML.hpp

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

#include <QObject> 

namespace bb { namespace cascades { class Application; }} 

/*! 
* @brief Application pane object 
* 
*Use this object to create and init app UI, to create context objects, to register the new    meta types etc. 
*/ 
class CalcolatorQML : public QObject 
{ 
Q_OBJECT 
public: 
CalcolatorQML(bb::cascades::Application *app); 
virtual ~CalcolatorQML() {} 
Q_INVOKABLE void injectContainer(); 
}; 



#endif /* CalcolatorQML_HPP_ */ 

這是我的main.qml,你可以在b1中看到這個語句injection.injectContainer1();

import bb.cascades 1.0 


Page { 
content: Container { 
    Container { 
     layout: StackLayout { 
     } 
     TextField { 
      id: textFieldId 
     } 
     Container { 
      layout: StackLayout { 
       orientation: LayoutOrientation.LeftToRight 
      } 
      verticalAlignment: VerticalAlignment.Center 
      horizontalAlignment: HorizontalAlignment.Center 
      Button { 
       id: b1 
       text: "1" 
       onClicked: { 
        injection.injectContainer(); 
       } 
      } 
      Button { 
       id: b2 
       text: "2" 
      } 
      Button { 
       id: b3 
       text: "3" 
      } 
     } 
     Container { 
      layout: StackLayout { 
       orientation: LayoutOrientation.LeftToRight 
      } 
      verticalAlignment: VerticalAlignment.Center 
      horizontalAlignment: HorizontalAlignment.Center 
      Button { 
       id: b4 
       text: "4" 
      } 
      Button { 
       id: b5 
       text: "5" 
      } 
      Button { 
       id: b6 
       text: "6" 
      } 
     } 
     Container { 
      layout: StackLayout { 
       orientation: LayoutOrientation.LeftToRight 
      } 
      verticalAlignment: VerticalAlignment.Center 
      horizontalAlignment: HorizontalAlignment.Center 
      Button { 
       id: b7 
       text: "7" 
      } 
      Button { 
       id: b8 
       text: "8" 
      } 
      Button { 
       id: b9 
       text: "9" 
      } 
     } 
     Container { 
      layout: StackLayout { 
       orientation: LayoutOrientation.LeftToRight 
      } 
      verticalAlignment: VerticalAlignment.Center 
      horizontalAlignment: HorizontalAlignment.Center 
      Button { 
       id: b0 
       text: "0" 
      } 
     } 
     Container { 
      layout: StackLayout { 
       orientation: LayoutOrientation.LeftToRight 
      } 
      verticalAlignment: VerticalAlignment.Center 
      horizontalAlignment: HorizontalAlignment.Center 
      Button { 
       id: bPlus 
       text: "+" 
      } 
      Button { 
       id: bMult 
       text: "*" 
      } 
      Button { 
       id: bEqual 
       text: "=" 
      } 
      Button { 
       id: bDivide 
       text: "/" 
      } 
      Button { 
       id: bMinus 
       text: "-" 
      } 
     } 
    } 
} 
}// end of Page 

我的問題不是調用 感謝

回答

4

在CalcolatorQML.cpp,前行的功能injectContainer

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

地址:

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

然後內onClicked在您的QML中,請嘗試:

app.injectContainer(); 
+0

我已經這樣做,但仍然無法在我的應用程序工作。 –

1

試試這個

AbstractPane *root = qml->createRootObject<AbstractPane>(); 
qml->setContextProperty("root", this); 

在的onClick設爲Qml寫

root.injectContainer(); 
+0

AbstractPane * root = qml-> createRootNode (); qml-> setContextProperty(「root」,this);在qml文件中root.injectContainer(); – pranavjayadev