Not able to connect a c++ signal to a slot in QML

438 views Asked by At

Hi Everyone i have a foo class Code in Foo.h

Class Foo : public QObject
{
    static Foo* Get(); // Singleton Pattern

    Q_INVOKABLE int addFoo();

signals:
    void fooAdded(int index);

public slots:

private:
    QList<QString> m_fooArray;
}

Code in foo.cpp

int Foo::addFoo()
{
    QString newString = "blah.."
    m_fooArray.append(newString);

    emit fooAdded(m_fooArray.size()-1);

    return m_fooArray.size()-1;
}

in my main function code is

{
    Foo fooInstance;
    qmlRegisterType<GUI::Foo>("xxx", 1, 0, "Foo");
    engine.rootContext()->setContextProperty("fooInstance", &fooInstance);
}

Now i have two xml main.xml and fooTabView.qml

in main.xml code is

 import QtQuick 2.3
 import QtQuick.Controls 1.2
 import xxx 1.0

ApplicationWindow {
    id: appWindow
    visible: true
    width: 640
    height: 480

    FooTabView {
        anchors.left: parent.left
        anchors.top: parent.top
        anchors.margins: 50
    }

    Button {
        id: addFoo
        x: 50
        y: 59
        text: qsTr("Add Connection")
        onPressedChanged:  fooInstance.addFoo();
    }
}

Now when i compile and debug when i click on the addFoo button my breakpoint in addFoo() function gets hit and the signal fooAdded(index) is emitted

and in my FooTabView.qml i have the below code

     import xxx 1.0
     .....

     .....
     Foo.onFooAdded: 
     {
     .....
     }

But now when i compile and debug i get no error but my breakpoint inside Foo.onFooAdded never get hits.

What i basically want is that when i hit that button a new foo get added to the list and than i will have my logic to create a new tab inside Foo.onFooAdded { ... }

Can someone please tell me what i am missing or doing wrong and how can i connect the fooAdded(index) signal to onFooAdded inside QML.

please note i have also tried the below code also inside FooTabView.xml but still my breakpoint did not got hit

Connections {
    target: fooInstance
    onFooAdded: {
    ...
    }
}

I think i somehow have to connect to fooInstance::onFooAdded and not Foo::onFooAdded but i get compile error if i change to fooInstance::onFooAdded

Thanks in Advance

3

There are 3 answers

0
mpcarlos87 On

¿Could you try catching the events this way from the QML?

//QML
fooInstance{
    onFooAdded: {

    }
}

I have some code in Github with plenty of examples that maybe can help you, because signals and slots from QML<->C++ are sometimes a little annoying to set up. The code is documented, but any doubt just ask ;)

Hope this helps!

1
QtRoS On

and in my FooTabView.qml i have the below code

import xxx 1.0
.....

.....
Foo.onFooAdded: 
{
.....
}

This part looks like the using of attached signal, not like object's signal. Try to setup connection via such call:

fooInstance.fooAdded.connect(someJsFunction)
0
tonytony On

Using Connections is the correct way of connecting to context properties' slots. I created a test project similar to yours and everything works fine.

I don't completely trust QML breakpoints so I suggest you use console.log("...") statement to check whether slot gets called or not.