我是黑莓10开发的新手。我已经创建了简单的BB 10级联项目。 我想通过C++函数更改标签的文本。如何从qml调用C++函数并更改标签文本
main.qml
import bb.cascades 1.0
Page {
content: Container {
id: containerID
Button {
id: button1
objectName: "button"
text: "text"
onClicked: {
btnClicked("New Label Text");
}
}
Label {
id: label1
objectName: "label1"
text: "Old Label Text"
}
}
}
现在在哪个文件我已经申报并在哪个文件我已经定义函数btnClicked(QString的)功能。
HelloBB.hpp
// Default empty project template
#ifndef HelloBB_HPP_
#define HelloBB_HPP_
#include <QObject>
namespace bb { namespace cascades { class Application; }}
class HelloBB : public QObject
{
Q_OBJECT
public:
HelloBB(bb::cascades::Application *app);
virtual ~HelloBB() {}
};
#endif
HelloBB.cpp
// Default empty project template
#include "HelloBB.hpp"
#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
using namespace bb::cascades;
HelloBB::HelloBB(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);
qml->setContextProperty("app", this);
// create root object for the UI
AbstractPane *root = qml->createRootObject<AbstractPane>();
// set created root object as a scene
app->setScene(root);
}
现在我想从旧标签文本标签文本更改为用户给定文本。我从qml调用C++函数。我不知道在哪里定义这个函数,以及如何从qml连接这个C++函数。
谢谢。