SIGNAL / SLOT problem

jeffreyg
jeffreyg used Ask the Experts™
on
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::~QSocketClass()
{
  printf("In the QSocketClass destructor\n");
  dataStream.unsetDevice();

  theApp->socket->close();
}

bool QSocketClass::connectSocket(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->connectToHost(host, port);
  dataStream.setDevice(theApp->socket);

  return(true);
}

void QSocketClass::socketReadyRead()
{
  QCString s;
  s.resize(theApp->socket->bytesAvailable());
  theApp->socket->readBlock(s.data(), theApp->socket->bytesAvailable());

  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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2006

Commented:
Ok, i got your problem ... took me a while to figure out but I think I have identified it ..

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 ...

Commented:
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.
Commented:
Okay back again, i reviewed it and noticed something VERY important! You don't have Q_OBJECT in your class prototype! Add it and it will work (perhaps you also have to make the slot protected though)

Good luck! :)

Author

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial