没有与QObject :: connect匹配的功能 [英] No matching function for QObject::connect

查看:89
本文介绍了没有与QObject :: connect匹配的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我有一个客户端类:

//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 中使用它:

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

我遇到了错误:

没有用于调用'QObject :: connect(QTimer *& ;, const char *,clientSupervision * const,const char *)的匹配函数

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

我不明白为什么connect函数找不到匹配的函数.

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

我应该改变什么?

推荐答案

一般来说,可能有多个原因会导致该问题:

There can be several reasons for the issue in general:

  • 您不继承QObject.

  • You do not inherit QObject.

您的班级中没有Q_OBJECT宏.

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

您应该将代码更改为:

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) { ... }

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

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

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

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.

这篇关于没有与QObject :: connect匹配的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆