jeffreyg
asked on
SIGNAL / SLOT problem
I am a newbie to KDevelop (2 weeks) and I am trying to write a class that accepts data from a socket. I have implemented the following code:
// Header file
#include <qsocket.h>
class QSocketClass : public QSocket
{
public:
QSocketClass();
~QSocketClass();
bool connectSocket(QString &host, Q_UINT16 port);
void sendToServer(QString str);
void ProcessPendingRead();
void closeSocket();
private slots:
void socketReadyRead();
private:
QDataStream dataStream;
};
// C++ file
#include "KioskDemo.h"
#include "Socket.h"
extern KioskDemo *theApp;
QSocketClass::QSocketClass ()
{
printf("In the QSocketClass constructor\n");
}
QSocketClass::~QSocketClas s()
{
printf("In the QSocketClass destructor\n");
dataStream.unsetDevice();
theApp->socket->close();
}
bool QSocketClass::connectSocke t(QString &host, Q_UINT16 port)
{
printf("trying to connect to the socket host - " + host + "\n");
if (!theApp->socket)
{
theApp->AfxMessageBox("Out of memory");
return(false);
}
connect(theApp->socket, SIGNAL(readyRead()), this, SLOT(socketReadyRead()));
theApp->socket->connectToH ost(host, port);
dataStream.setDevice(theAp p->socket) ;
return(true);
}
void QSocketClass::socketReadyR ead()
{
QCString s;
s.resize(theApp->socket->b ytesAvaila ble());
theApp->socket->readBlock( s.data(), theApp->socket->bytesAvail able());
printf(s + "\n");
return;
}
void QSocketClass::sendToServer (QString str)
{
// write to the server
printf("Sending to socket server : " + str + "\n");
dataStream.writeBytes(str, str.length());
return;
}
Now this compiles and I can send information to the socket port and the application at the other end picks it up and responds by squiting data back.
However I get the following message when I run the app :
QObject::connect: No such slot QSocket::socketReadyRead()
QObject::connect: (sender name: 'unnamed')
QObject::connect: (reciever name: 'unnamed')
I tried a demo on the web : http://doc.trolltech.com/3.0/mail-example.html that incorporated the readyRead() method and it works but I cannot see the difference between the two app in terms of this readyRead() function. Is this because it inherits from QSocket? how can I force it to look at the QSocketClass for the function and not search in QSocket?
regards in advance,
Gordon.
// Header file
#include <qsocket.h>
class QSocketClass : public QSocket
{
public:
QSocketClass();
~QSocketClass();
bool connectSocket(QString &host, Q_UINT16 port);
void sendToServer(QString str);
void ProcessPendingRead();
void closeSocket();
private slots:
void socketReadyRead();
private:
QDataStream dataStream;
};
// C++ file
#include "KioskDemo.h"
#include "Socket.h"
extern KioskDemo *theApp;
QSocketClass::QSocketClass
{
printf("In the QSocketClass constructor\n");
}
QSocketClass::~QSocketClas
{
printf("In the QSocketClass destructor\n");
dataStream.unsetDevice();
theApp->socket->close();
}
bool QSocketClass::connectSocke
{
printf("trying to connect to the socket host - " + host + "\n");
if (!theApp->socket)
{
theApp->AfxMessageBox("Out
return(false);
}
connect(theApp->socket, SIGNAL(readyRead()), this, SLOT(socketReadyRead()));
theApp->socket->connectToH
dataStream.setDevice(theAp
return(true);
}
void QSocketClass::socketReadyR
{
QCString s;
s.resize(theApp->socket->b
theApp->socket->readBlock(
printf(s + "\n");
return;
}
void QSocketClass::sendToServer
{
// write to the server
printf("Sending to socket server : " + str + "\n");
dataStream.writeBytes(str,
return;
}
Now this compiles and I can send information to the socket port and the application at the other end picks it up and responds by squiting data back.
However I get the following message when I run the app :
QObject::connect: No such slot QSocket::socketReadyRead()
QObject::connect: (sender name: 'unnamed')
QObject::connect: (reciever name: 'unnamed')
I tried a demo on the web : http://doc.trolltech.com/3.0/mail-example.html that incorporated the readyRead() method and it works but I cannot see the difference between the two app in terms of this readyRead() function. Is this because it inherits from QSocket? how can I force it to look at the QSocketClass for the function and not search in QSocket?
regards in advance,
Gordon.
Okay, the problem most likely has to do with the slot socketReadyDead() being private. You have to make it protected, or even public.
Let me know if this was indeed the problem, since I don't have the oppertunity to reproduce the problem at the moment.
Let me know if this was indeed the problem, since I don't have the oppertunity to reproduce the problem at the moment.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Remenic,
Seeing u talk about the Q-OBJECT I placed the readyRead() event in the main application windows and have passed my socket message through the app into the relevant sub application. Also made the readyRead() a public SLOT.
regards,
Gordon.
Seeing u talk about the Q-OBJECT I placed the readyRead() event in the main application windows and have passed my socket message through the app into the relevant sub application. Also made the readyRead() a public SLOT.
regards,
Gordon.
there is no member called socket in your class ... also you are deriving from the QSocket which has a member called socket ...
since you are receiving signal on socket, the SLOT cannot be at the child ( it will not be visible to the parent )
to get rid of the problem, add a member ot type QSocket called socket in your QSocketClass and restructure just like shown in the SMTP example which you have posted ...