Home:ALL Converter>No matching function for QObject::connect

No matching function for QObject::connect

Ask Time:2014-06-25T21:30:12         Author:Evans Belloeil

Json Formatter

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?

Author:Evans Belloeil,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/24410130/no-matching-function-for-qobjectconnect
László Papp :

There can be several reasons for the issue in general:\n\n\nYou do not inherit QObject.\nYou do not have the Q_OBJECT macro in your class.\nYou do not define the method as slot in your header file where the class is declared.\n\n\nYour issue is the first which can be seen here:\n\nclass clientSupervision\n\n\nYou should change your code to:\n\nclass clientSupervision : public QObject\n// ^^^^^^^^^^^^^^^^\n\n\nOf course, the constructor implementation and signature would need to change, too, as follows:\n\nexplicit clientSupervision(QObject *parent = Q_NULL_PTR) : QObject(parent) { ... }\n\n\nIn addition, you seem to leak your QTimer instance as it does not get the parent as a parameter to the constructor.\n\nFurthermore, the QObject:: scope is needless in your code as your class ought to inherit QObject directly or indirectly either way.\n\nEven more, I would highly encourage you to utilize the new signal-slot syntax.",
2014-06-25T13:31:49
Peter Mitrano :

Another possible cause of this error is trying to connect to a slot which is overloaded. For example, this well cause the same error\n\nQObject::connect(this,\n &MazeWidget::MyUpdate,\n this, \n &QWidget::update,\n Qt::QueuedConnection);\n\n\nBut not if you explicitely cast:\n\nQObject::connect(this,\n &MazeWidget::MyUpdate,\n this,\n static_cast<void (QWidget::*)()>(&QWidget::update),\n Qt::QueuedConnection);\n",
2017-09-11T03:56:55
2NinerRomeo :

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.",
2018-11-30T00:47:35
yy