c++ - QObject::connect 没有匹配函数

标签 c++ qt qtcore qobject qt-signals

我正在编写一个程序,每 10 毫秒发送一个 UDP 帧。以下是我的程序应该如何工作:

我有一个客户端类:

//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();
}

我的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

然后我在我的 main 中使用它:

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

    return a.exec();
}

我遇到了错误:

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

我不明白为什么连接函数找不到匹配的函数。

我应该改变什么?

最佳答案

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

  • 您不继承 QObject。

  • 您的类中没有 Q_OBJECT 宏。

  • 您没有在声明类的头文件中将方法定义为插槽。

您的问题是第一个可以在这里看到的问题:

class clientSupervision

您应该将代码更改为:

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

当然,构造函数的实现和签名也需要改变,如下所示:

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

此外,您似乎泄漏了 QTimer 实例,因为它没有将父对象作为构造函数的参数。

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

此外,我强烈建议您使用 the new signal-slot syntax .

关于c++ - QObject::connect 没有匹配函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24410130/

相关文章:

c++ - 将 QBitArray 转换为 QByteArray

python - 更改 QTableWidgetItem 背景颜色

c++ - 将图片从在线保存到本地存储qt 5.1

c++ - QPolarChart 的反轴

c++ - 将 C++ 文件解析为 XML 文件

c++ - rdbuf(...) 返回一个指针——谁拥有指向的缓冲区?

c++ - 我是否需要在 CUDA 中跨多个 GPU 镜像输入缓冲区/纹理?

c++ - 在 Qt 中获取系统用户名

c++ - Qt永久删除文件

c++ - 如何在 C++ 中实现一个函数作为一些可选参数?