0

I'm trying to implement a custom class of QTcpSocket but it seems that my slots are not recognized at run time I always get:

Object::connect: No such slot QTcpSocket::timeoutSlot()

Here is my code:

my header:

#ifndef CUSTOM_SOCKET_H
#define CUSTOM_SOCKET_H

#include <QTcpSocket>
#include <QTimer>

class CustomSocket : public QTcpSocket {
    Q_OBJECT
public:
    CustomSocket(QObject* = 0);

private:
    QTimer *mAuthTimeout;

public slots:
    void timeoutSlot();

};
#endif

Implementation:

#include "customSocket.h"

CustomSocket::CustomSocket(QObject *aParent):QTcpSocket(aParent)
{
  mAuthTimeout = new QTimer();
  connect(mAuthTimeout, SIGNAL(timeout()), this, SLOT(timeoutSlot()));
  mAuthTimeout->start(5000);

}

void CustomSocket::timeoutSlot(){
  std::cout << "Timeout " << std::endl;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
rednaks
  • 1,982
  • 1
  • 17
  • 23

2 Answers2

0

There is nothing wrong with the code quoted above.

The warning you get has 2 weird things about it;

  • it complains about QTcpSocket::timeoutSlot(). We are not trying to connect to QTcpSocket, but to CustomSocket. The warning should mention this. If it doesn't then the Q_OBJECT macro may be missing.
  • if I copy/paste the code and add it to an empty dir, add main(), then use qmake -project; qmake; make. It works just fine. No warning.
Thomas
  • 1
0

Looking at the QTcpSocket class, it's declared with the Q_DISABLE_COPY macro. This possibly could be causing the errors you're seeing.

Whether or not that is the case, I think you'd be better off not inheriting from QTcpSocket, but instead creating an instance in your class: -

class CustomSocket : public QObject
{
    Q_OBJECT

    private:
        QTCPSocket* m_pSocket;    
};


CustomSocket::CustomSocket(QObject* parent)
    :QObject(parent)
{
    m_pSocket = new QTcpSocket(this);
}

You should then be Ok to connect to the QTcpSocket and use its signals and slots to communicate with it as normal.

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85