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

Posts: 13
Registered: ‎04-25-2011
My Device: BlackBerry PlayBook
My Carrier: Wi-Fi

Emit signal from dynamic component and handle it in C++

I have a custom component which is created dynamically in QML using ComponentDefinition (i.e. the instance of this QML component does not exist when the app is first started). 


I want to emit a custom signal from this component and be able to handle it in my app.cpp code in C++.


How can i do this?

Posts: 45
Registered: ‎01-18-2013
My Device: 9000
My Carrier: CMCC

Re: Emit signal from dynamic component and handle it in C++


the same qustion i wondered

Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Emit signal from dynamic component and handle it in C++



I've created a sample project that shows how to capture signals emitted by dynamically created QML components and how to differentiate between their instances:



Click "Create CustomComponent" button multiple times to create different components.

Click other buttons to emit signals which will be captured and processed in C++ code.


Important points:

- Signal should be declared at the top level of the component otherwise it won't be found.

- Ensure that you're connecting the signal to a C++ object AFTER creating the component. I call a C++ function after creating the component to do this.





import bb.cascades 1.0

Page {
    property int nextId: 1;
    ScrollView {
        Container {
            id: container
            Label {
                objectName: "testLabel"
                multiline: true
            Button {
                text: "Create CustomComponent"
                onClicked: {
                    var comp = customComponentFactory.createObject();
                    comp.componentId = nextId++; 
            attachedObjects: [
                ComponentDefinition {
                    id: customComponentFactory
                    content: CustomComponent { }



import bb.cascades 1.0

Container {
    signal buttonClicked(string first, string second)
    property string componentId;
    Button {
        text: "Click me to emit a signal"
        onClicked: {
            buttonClicked("Param1", "Param2");



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

    Q_INVOKABLE void subscribeToSignals(QObject *object);

public slots:
    void processButtonClick(const QString &param1, const QString &param2);



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

    qml->setContextProperty("app", this);
    AbstractPane *root = qml->createRootObject<AbstractPane>();

void Test::subscribeToSignals(QObject *object)
    QObject::connect(object, SIGNAL(buttonClicked(QString, QString)),
    		this, SLOT(processButtonClick(QString, QString)));

void Test::processButtonClick(const QString &param1, const QString &param2)
	QObject *customComponent = sender();
	Label *testLabel = Application::instance()->scene()->findChild<Label *>("testLabel");
	if (testLabel)
		testLabel->setText(testLabel->text() + "\n" +
				"ID: " + customComponent->property("componentId").toString() +
				", param1: " + param1 + ", param2: " + param2);






Andrey Fidrya, @zmeyc on twitter