Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I`m facing an error while connecting a signal to a slot in my project, it is:

<pre>error: no matching function for call to ‘QObject::connect(A&, const char [12], Handler&, const char [12])


My Project is quite big, and I can`t post the whole code here, but what I have done is:

I have class A, which dynamically allocates an instance of class B when a method is called, and emits a signal which carries the instance of B.

In another class named handler, I have a slot which recives an object of type B.

And in the main function, I connect them like this:

connect(a, SIGNAL(changed(B*)), handler, SLOT(process(B*)));

I have made a similar small program which produces this error, would be glad if you took a look: here

Thanks in advance.

What I have tried:

C++
#include <QObject>
#include <QDebug>
#include <QCoreApplication>


class A : public QObject
{
    Q_OBJECT
public:
    void set(int v)
    {
        b = new B(v);
        emit wasSet(b);
    }
    
signals:
    void wasSet(B *b);
    
private:
    B *b;
};

class B
{
public:
    B(int v)
    {
        qDebug() << v;
        qDebug() << "Initialized.";
    }
};

class Handler : public QObject
{
    Q_OBJECT
public slots:
    void handle(B *b)
    {
        qDebug() << "Object handled.";
    }
};

int main(int argc, char* argv[])
{
    QCoreApplication app(argc, argv);
    A a;
    Handler h;
    QObject::connect(a, SIGNAL(wasSet(B*)), h, SLOT(handle(B*)));
    a.set(12);
    return app.exec();
}
Posted
Updated 22-Aug-17 21:38pm

1 solution

Have a look at the declaration of the static connect function:
C++
connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type = Qt::AutoConnection)
You have to pass pointers for the sender and receiver QObject but you are passing by reference:
A a;
Handler h;
QObject::connect(a, SIGNAL(wasSet(B*)), h, SLOT(handle(B*)));
Solution:
QObject::connect(&a, SIGNAL(wasSet(B*)), &h, SLOT(handle(B*)));
 
Share this answer
 
Comments
Ali-RNT 23-Aug-17 9:09am    
Ah really thanks! I thought the problem was something more serious :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900