Hi List,
I'm trying to add a slot to my very simple HTTP server using QThread.
First, I tried with this simple code :
---------------- serverthread.h ---------------
#ifndef SERVERTHREAD_H
#define SERVERTHREAD_H
#include <QThread>
class ServerThread : public QThread
{
public:
ServerThread( int descriptor, QObject *parent );
void run();
public slots:
void onReadReady();
private:
int m_descriptor;
QTcpSocket * ourSocket;
};
#endif // SERVERTHREAD_H
---------------------------------------------------------
---------------- serverthread.cpp ---------------
#include <QtNetwork>
#include "serverthread.h"
ServerThread::ServerThread( int descriptor, QObject *parent ) :
QThread( parent )
{
m_descriptor = descriptor;
}
void ServerThread::run()
{
QTcpSocket socket;
if( ! socket.setSocketDescriptor( m_descriptor ) )
{
qDebug( "Socket error!" );
return;
}
ourSocket = & socket;
connect (ourSocket, SIGNAL (readyRead()), this, SLOT (onReadyRead ()));
exec ();
socket.disconnectFromHost ();
socket.waitForDisconnected ();
ourSocket = NULL;
}
void ServerThread::onReadReady()
{
qDebug() << "Connexion from host:" << ourSocket -> peerAddress () .
toString ();
}
---------------------------------------------------------
Now running the server :
./tcpserver
When calling the server, I got this error message :
./tcpserver
Object::connect: No such slot QThread::onReadyRead()
After googling a bit, I found that I've to add the macro "Q_OBJECT" to my
thread class.
So, it became :
---------------- serverthread.h ---------------
#ifndef SERVERTHREAD_H
#define SERVERTHREAD_H
#include <QThread>
class ServerThread : public QThread
{
Q_OBJECT
public:
ServerThread( int descriptor, QObject *parent );
void run();
public slots:
void onReadReady();
private:
int m_descriptor;
QTcpSocket * ourSocket;
};
#endif // SERVERTHREAD_H
---------------------------------------------------------
Now, I got a compile problem to fix :
serverthread.h:19: error: ISO C++ forbids declaration of 'QTcpSocket' with
no type
serverthread.h:19: error: expected ';' before '*' token
make: *** [moc_serverthread.o] Error 1
Line 19 is related to the declaration of :
QTcpSocket * ourSocket;
which compiles just fine in my first test without "Q_OBJECT".
I'm completly lost.
Any advices please ?
Thanks in advance
N.B: I've tested it on QT 4.4.0 and 4.3.4 and I've faced the same problem.
Regards
F.
_______________________________________________
Qt4-preview-feedback mailing list
[email protected]
http://lists.trolltech.com/mailman/listinfo/qt4-preview-feedback