我正在编写一个程序,每 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/