No matching function for QObject::connect
Asked Answered
A

4

20

I'm writing a program that send an UDP frame every 10 mS. Here's how my program is supposed to work :

I've got a client class :

//Constructor
clientSupervision::clientSupervision()
{
}

void clientSupervision::sendDataUDP(){
    //Create a frame and send it
...
}

void clientSupervision::sendDataUDPTimer(int timer){
    QTimer *tempsEnvoieTrameSupervision = new QTimer();//Create a timer
    tempsEnvoieTrameSupervision->setInterval(timer);//Set the interval

    //Mise en place des connections
    QObject::connect (tempsEnvoieTrameSupervision,SIGNAL (timeout()),this, SLOT (envoiTrameSupervision())); //Connect the timer to the function
    tempsEnvoieTrameSupervision->start();// Start the timer
}

//Call sendDataUDP
void clientSupervision::envoiTrameSupervision(){
    std::cout << "Envoi de la trame de supervision";
    sendDataUDP();
}

My header file of clienSupervision.h :

#ifndef CLIENTSUPERVISION_H
#define CLIENTSUPERVISION_H
#include <winsock2.h> // pour les fonctions socket
#include <cstdio> // Pour les Sprintf
#include "StructureSupervision.h"
#include "utilitaireudp.h"
#include <QTimer>
#include <QObject>
#include <iostream>
class clientSupervision
{
    Q_OBJECT
public:
    clientSupervision();
    void sendDataUDP();
    void sendDataUDPTimer(int timer);

public slots:
    void envoiTrameSupervision();
};

#endif // CLIENTSUPERVISION_H

Then I use this in my main :

int main(int argc, char *argv[])
{
    clientSupervision c;
    c.sendDataUDPTimer(10);
    QCoreApplication a(argc, argv);

    return a.exec();
}

I've got the error :

no matching function for call to 'QObject::connect(QTimer*&, const char*, clientSupervision* const, const char*)

I don't understand why the connect function can't find a matching function.

What should I change?

Aerophagia answered 25/6, 2014 at 13:30 Comment(4)
Your class should inherit QObject. Otherwise Qt signal/slot mechanism will not work.Snowdrift
@FinalContest Thanks for your help, I will se you soon, I have another question on that code ^^Aerophagia
@EvansBelloeil: sure, np. Do not worry about the downvotes we got. They are all unexplained, hence nothing to worry about. :)Kempf
@ipapp I agree with you as well. Unexplained downvotes usually mean nothing to worry about.Underprop
B
18

There can be several reasons for the issue in general:

  • You do not inherit QObject.

  • You do not have the Q_OBJECT macro in your class.

  • You do not define the method as slot in your header file where the class is declared.

Your issue is the first which can be seen here:

class clientSupervision

You should change your code to:

class clientSupervision : public QObject
//                      ^^^^^^^^^^^^^^^^

Of course, the constructor implementation and signature would need to change, too, as follows:

explicit clientSupervision(QObject *parent = Q_NULL_PTR) : QObject(parent) { ... }

In addition, you seem to leak your QTimer instance as it does not get the parent as a parameter to the constructor.

Furthermore, the QObject:: scope is needless in your code as your class ought to inherit QObject directly or indirectly either way.

Even more, I would highly encourage you to utilize the new signal-slot syntax.

Bruch answered 25/6, 2014 at 13:31 Comment(2)
This change my error with : undefined reference to `vtable for clientSupervision'Aerophagia
@EvansBelloeil: you need to properly generate the moc, but that is another question. If you use qmake this ought to work, otherwise include "foo.moc" at the end of the source file of yours. You might also need to get a ~clientSupervision() destructor.Kempf
M
9

Another possible cause of this error is trying to connect to a slot which is overloaded. For example, this well cause the same error

QObject::connect(this,
                 &MazeWidget::MyUpdate,
                 this, 
                 &QWidget::update,
                 Qt::QueuedConnection);

But not if you explicitely cast:

QObject::connect(this,
                 &MazeWidget::MyUpdate,
                 this,
                 static_cast<void (QWidget::*)()>(&QWidget::update),
                  Qt::QueuedConnection);
Mas answered 11/9, 2017 at 3:56 Comment(0)
E
3

Here's another one that snuck up on me: The class of the slot object had been forward declared in the header, but not defined in the implementation by including its header.

Euthanasia answered 30/11, 2018 at 0:47 Comment(0)
A
0

If none of the answers above worked, check if you have assigned correct object to the signals and the slots. You will get this error when the signals and slots are valid and refer to one object but the object assigned to that signal and slot is different.

Ameliaamelie answered 10/8, 2020 at 19:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.