从 C++ 调用 QML 函数 [英] Calling a QML function from C++

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

问题描述

我正在使用 BB Native SDK 开发 Blackberry 10 移动应用程序.

I'm developing a Blackberry 10 mobile application using the BB Native SDK.

我需要在我的 C++ 类中调用 QML 函数.我对此进行了大量搜索,但我只发现将 C++ 调用到 QML 中的可能性,而不是相反.您可以查看:QML 和 C++ 集成

I need to call a QML function into my C++ class. I searched a lot on this but I only found the possibility to call C++ into QML not the inverse. You can check this: QML and C++ integration

有人可以帮我吗?

这是指定我需要调用的将标记添加到我的地图视图中的函数的 QML 代码:

This is the QML code specifying the function that I need to call which add a marker into my mapview:

Container {
    id: pinContainer
    objectName: "pinContObject"
        ...

        function addPin(lat, lon, name, address) {
            var marker = pin.createObject();
            marker.lat = lat;
            marker.lon = lon;
            ...
        }
}

推荐答案

这就是信号和槽的用途.您可以使用 QML Connections 将任意信号连接到 QML 中的任意槽.

Thats what signals and slots are for. You can use the QML Connections for connecting arbitrary signals to arbitrary slots in QML.

http://qt-project.org/doc/qt-4.8/qml-connections.html

  Container {
            id: pinContainer
            objectName: "pinContObject"
            ...

            function addPin(lat, lon, name, address) {
                var marker = pin.createObject();
                marker.lat = lat;
                marker.lon = lon;
                ...
            }

            Connections {
                target: backend
                onDoAddPin: { 
                  addPin(latitude, longitude,name, address)
                }
            }
     }

而在 C++ 后端,您所要做的就是

and in C++ backend, all you have to do is

class Backend: public QObject {
signals:
    void doAddPin(float latitude, float longitude, QString name, QString address);

 ........
 void callAddPinInQML(){
     emit doAddPin( 12.34, 45.67, "hello", "world");
 }
}

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

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