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

查看:345
本文介绍了从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连接将任意信号连接到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天全站免登陆