Я хочу вызвать функцию C ++ из Cascade 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.0Page {
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
Моя проблема в функции injectContainer
не вызывается
Спасибо
В CalcolatorQML.cpp, до линия:
AbstractPane *root = qml->createRootObject<AbstractPane>();
добавлять:
qml->setContextProperty("app", this);
Затем в onClicked в вашем QML попробуйте:
app.injectContainer();
Попробуй это
AbstractPane *root = qml->createRootObject<AbstractPane>();
qml->setContextProperty("root", this);
В Qml onClick пишите
root.injectContainer();