连接:没有这样的slot QObject::processPendingDatagrams()[副本]

时间:2020-11-27 18:48:47

This question already has an answer here:

这个问题已经有了答案:

I have written an UDP programme with Qt, and when I connect this: connect(socket,SIGNAL(readyRead()),this,SLOT(processPendingDatagrams()));

我已经编写了一个带有Qt的UDP方案,当我将其连接起来时:connect(socket,SIGNAL(readyRead()),this,SLOT(processPendingDatagrams()));

the complier tells me that no such slot

编译器告诉我没有这样的槽。

the error click here

错误的点击这里

and I want to know how to fix it, thank you!

我想知道如何修理它,谢谢!

P.S. Here are my files:

这里是我的档案:

files

文件

Here are my codes:

下面是我的代码:

enter code here udptest.cpp:

在这里输入代码udptest.cpp:

#include "udptest.h"

#include <QObject>

#include <QUdpSocket>

#include <QtNetwork>

UDPtest::UDPtest()
{
    socket = new QUdpSocket();

    port = 2016;
    socket->bind(port,QUdpSocket::ShareAddress
                                    | QUdpSocket::ReuseAddressHint);

       connect(socket,SIGNAL(readyRead()),this,SLOT(processPendingDatagrams()));

}

QString UDPtest::getIP() 
{
    QList<QHostAddress> list = QNetworkInterface::allAddresses();
    foreach (QHostAddress address, list)
    {
       if(address.protocol() == QAbstractSocket::IPv4Protocol) 
            return address.toString();  
    }
       return 0;
}








void UDPtest::sendMessage(QString message) 
{
    QByteArray data;
    QDataStream out(&data,QIODevice::WriteOnly);
    QString localHostName = QHostInfo::localHostName();
    QString address = getIP();
    out <<"123"<< localHostName << address << message;
    socket->writeDatagram(data,data.length(),QHostAddress::Broadcast, port);

}



void UDPtest::processPendingDatagrams() 
{qDebug()<<"receive";
    while(socket->hasPendingDatagrams())
    {
        QByteArray datagram;
        datagram.resize(socket->pendingDatagramSize());
        socket->readDatagram(datagram.data(),datagram.size());
        QDataStream in(&datagram,QIODevice::ReadOnly);
        QString userName,localHostName,ipAddress,message;
        QString time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
         in >>userName >>localHostName >>ipAddress >>message;
         QString msg=time+userName+localHostName+ipAddress+message;
         msger=msg;
         qDebug()<<msg;
      }
}

QString UDPtest:: messager()
{
    return msger;
}

main.cpp:

main.cpp:

    #include"udptest.h"
    #include<QDebug>
    #include <QtCore/QCoreApplication>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
     qDebug()<<"123";
    UDPtest test;
    test.sendMessage("aha");
    return a.exec();
}

udptest.h:

udptest.h:

#ifndef UDPTEST_H
#define UDPTEST_H


#include <QObject>
#include <QUdpSocket>
#include <QtCore/QCoreApplication>
#include <QtNetwork>

class UDPtest:public QObject
{

public:
    UDPtest();
    QString messager();
void sendMessage(QString);


private slots:
    void processPendingDatagrams();

private:

QString msger;
QUdpSocket *socket;
qint16 port;
QString getIP();

};

#endif // UDPTEST_H

QudptestConsole.pro:

QudptestConsole.pro:

    QT += core
QT -= gui
QT += network
CONFIG += c++11

TARGET = QudptestConsole
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp \
udptest.cpp

HEADERS += \
udptest.h

1 个解决方案

#1


1  

You have forgotten Q_OBJECT macro in UDPtest class

您已经忘记了UDPtest类中的Q_OBJECT宏。

class UDPtest: public QObject
{
    Q_OBJECT

public:
    UDPtest();

.....
}

#1


1  

You have forgotten Q_OBJECT macro in UDPtest class

您已经忘记了UDPtest类中的Q_OBJECT宏。

class UDPtest: public QObject
{
    Q_OBJECT

public:
    UDPtest();

.....
}