2016-05-04 169 views
1

我已經寫了Qt的UDP程序,當我連接這個: connect(socket,SIGNAL(readyRead()),this,SLOT(processPendingDatagrams()));的Qt:的QObject ::連接:沒有這樣的插槽的QObject :: processPendingDatagrams()

的編譯器告訴我, 沒有這樣的插槽

the error click here

我想知道如何解決它,謝謝!

P.S. 這裏是我的文件:

files

這裏是我的代碼:

輸入代碼在這裏 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中:

#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:

#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:

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 
+0

還請參閱http:// stackoverflow.com/a/26422155/142133 2 – Silicomancer

回答

1

你已經忘記Q_OBJECTUDPtest

class UDPtest: public QObject 
{ 
    Q_OBJECT 

public: 
    UDPtest(); 

..... 
} 
+0

非常感謝,但是當我添加Q_OBJECT時,它顯示錯誤:找不到架構的符號x89_64 – YOUNG

+0

@YOUG運行qmake,然後重新構建您的項目。 – thuga

相關問題