2

我想从 Cascade QML 调用 C++ 函数

这是计算器QML.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(); 这是 CalcatorQML.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

我的问题是函数injectContainer没有被调用 谢谢

4

2 回答 2

4

在 CalcatorQML.cpp 中,行

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

添加:

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

然后在 QML 中的 onClicked 中,尝试:

app.injectContainer();
于 2012-10-05T21:18:53.623 回答
1

试试这个

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

在 Qml onClick 中写入

root.injectContainer();
于 2012-10-25T07:56:50.420 回答