Search code examples
qtqtcpsocket

How to connect to QTcpSocket error signal in Qt


I am trying to get the QTcpSocket error by using signal and slot. I did like this:

connect(clientConnection->tcpSocket, &QTcpSocket::error, this, &ClientInterface::displayError);

void ClientInterface::displayError()
{
    qDebug() << clientConnection->tcpSocket->error();
    qDebug() << clientConnection->tcpSocket->errorString();
}

but I got this error:

error: no matching function for call to 'ClientInterface::connect(QTcpSocket*&, , ClientInterface*, void (ClientInterface::*)())' connect(clientConnection->tcpSocket, &QTcpSocket::error, this, &ClientInterface::displayError);

I also tried to implement the slot with QAbstractSocket::SocketError parameter like this:
void displayError(QAbstractSocket::SocketError);
But it showed the same error.

Where did I do wrong?

Edit:

I tried to connect like this as the answer says:

connect(clientConnection->tcpSocket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error), this, &ClientInterface::displayError);

But I got this problem:

QObject::connect: Cannot queue arguments of type 'QAbstractSocket::SocketError' (Make sure 'QAbstractSocket::SocketError' is registered using qRegisterMetaType().)


Solution

  • You need to overload the QAbstractSocket error:

    connect(tcpSocket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error),
    
                this, &ClientInterface::displayError);