2

I want to call C++ function from Cascade QML

this is 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");
}

and I declare the function as Q_INVOKABLE void injectContainer(); this is 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_ */

and this is my main.qml, you can see in b1 click this statement 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

My problem is the function injectContainer not invoked Thanks

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
JustMe
  • 732
  • 1
  • 11
  • 16

2 Answers2

4

In CalcolatorQML.cpp, before the line:

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

add:

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

Then within onClicked in your QML, try:

app.injectContainer();
Jace
  • 1,445
  • 9
  • 20
1

try this one

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

In the Qml onClick write

root.injectContainer();
pranavjayadev
  • 937
  • 7
  • 31