Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
varghesekiran
Posts: 38
Registered: ‎09-27-2012
My Device: 9930
My Carrier: airtel
Accepted Solution

Connect signal in cpp to slot in QML

Hi guys.,

Please help me with this

 

I am emitting a custom signal called imageLoaded from cpp which contains an image as parameter. How can i connect this signal to a slot in QML and can access this image,...?

 

Thanks in Advance...:smileysad:

Please use plain text.
Developer
sorout2009
Posts: 104
Registered: ‎08-23-2012
My Device: Blackberry Z10
My Carrier: TATA DOCOMO

Re: Connect signal in cpp to slot in QML

[ Edited ]

You can do like below

 

app.signalName.connect(YourPageID.imageLoaded);

 

here app is the class object being set as a property, signalName is the signal declared in cpp. imageLoaded is the function which is connected to your signal and will be called whenever you will emit the signal.

 

You can write this in onCreationComplete of the concerning page.

Dharmender Singh
Please use plain text.
Developer
YamilBracho
Posts: 713
Registered: ‎05-31-2010
My Device: 8520
My Carrier: Movistar

Re: Connect signal in cpp to slot in QML

For example :

import bb.cascades 1.0

Page {
    id: myPage
    Container {
        layout: DockLayout {}
        Label {
            id: myLabel
            text: qsTr("Hello World")
            textStyle.base: SystemDefaults.TextStyles.BigText
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Center
        }
        Button {
            text: "Press me"
            onClicked: {
                console.log("Boton clicked");
                cppObject.imageLoaded.connect(onImageLoaded);
                cppObject.executeCppFunction();
            }
             

        }
    }
    
    
    
    function onImageLoaded(message) {
        console.log("enter to slot in qml");
    	myLabel.text = message;    	        
    }
}


--------------------

#ifndef ApplicationUI_HPP_
#define ApplicationUI_HPP_

#include <QObject>

namespace bb { namespace cascades { class Application; }}

class ApplicationUI : public QObject
{
    Q_OBJECT
public:
    ApplicationUI(bb::cascades::Application *app);
    virtual ~ApplicationUI() {}

    Q_INVOKABLE void executeCppFunction();

signals:
	void imageLoaded(QString message);
};


#endif 


#include "applicationui.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>

using namespace bb::cascades;

ApplicationUI::ApplicationUI(bb::cascades::Application *app)
: QObject(app)
{
    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

	QmlDocument::defaultDeclarativeEngine()->rootContext()->setContextProperty("cppObject", this);

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

}


void ApplicationUI::executeCppFunction() {
	emit imageLoaded("Hello from C++ Signal");
}

 

Please use plain text.
Developer
varghesekiran
Posts: 38
Registered: ‎09-27-2012
My Device: 9930
My Carrier: airtel

Re: Connect signal in cpp to slot in QML

Thanks for your reply. It helps me a lot..:Clap:

Please use plain text.