QObject没有匹配函数::connect。

时间:2022-04-22 18:45:44

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

我正在编写一个程序,每10个ms发送一个UDP框架,这是我的程序应该如何工作的:

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 :

我的头文件的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*)

没有匹配的函数调用'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?

我该怎么改变?

2 个解决方案

#1


6  

There can be several reasons for the issue in general:

一般来说,这个问题有几个原因:

  • You do not inherit QObject.

    您不继承QObject。

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

    您的类中没有Q_OBJECT宏。

  • 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.

此外,您似乎会泄漏您的QTimer实例,因为它没有将父类作为参数传递给构造函数。

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

此外,QObject::在您的代码中,范围是不必要的,因为您的类应该直接或间接地继承QObject。

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

更重要的是,我强烈建议您使用新的signalslot语法。

#2


0  

Another possible cause of this error is trying to connect to a slot 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);

#1


6  

There can be several reasons for the issue in general:

一般来说,这个问题有几个原因:

  • You do not inherit QObject.

    您不继承QObject。

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

    您的类中没有Q_OBJECT宏。

  • 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.

此外,您似乎会泄漏您的QTimer实例,因为它没有将父类作为参数传递给构造函数。

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

此外,QObject::在您的代码中,范围是不必要的,因为您的类应该直接或间接地继承QObject。

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

更重要的是,我强烈建议您使用新的signalslot语法。

#2


0  

Another possible cause of this error is trying to connect to a slot 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);