如何从QML调用C ++函数并更改标签文本 [英] How to call c++ function from qml and change the lable text

查看:495
本文介绍了如何从QML调用C ++函数并更改标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Blackberry 10开发的新手.我创建了一个简单的BB 10级联项目. 我想通过c ++函数更改标签的文本.

I'm new to Blackberry 10 development. I've created simple BB 10 cascades project. I want to change the text of a label through c++ function.

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)函数.

Now in which file i've to declare and in which file i've to define the function btnClicked(QString) function.

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 ++函数.

Now I want to change the label text from Old Label Text to the user given text. I'm calling the c++ function from qml. I don't know where to define this function and how to connect this c++ function from qml.

谢谢.

推荐答案

您可以在此处找到有关集成C ++和QML的文档:

You can find the documentation for integrating C++ and QML here: http://developer.blackberry.com/cascades/documentation/dev/integrating_cpp_qml/

作为悬崖笔记:

在HelloBB构造函数中,您可以将类公开给QML,如下所示:

In your HelloBB constructor you can expose the class to the QML like so:

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

然后在C ++中创建一个可以从QML调用的方法.请记住,该方法必须标记为Q_INVOKABLE才能从QML调用.

And then create a method in the C++ that you will be able to call from the QML. Remember, the method has to be marked as Q_INVOKABLE to be called from the QML.

考虑一下:

Consider this:

       在HelloBB.hpp中:

        In HelloBB.hpp:

    public:
           Q_INVOKABLE void test();

       在HelloBB.cpp中:

        In HelloBB.cpp:

    void HelloBB::test() {
        qDebug() << "TEST";
    }

       在main.qml中:

        In main.qml:

   onClicked: {
       HelloBB.test ()
   }

这篇关于如何从QML调用C ++函数并更改标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆