Reputation: 1021
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().)
Upvotes: 2
Views: 9298
Reputation: 66
As already mentioned, you will need some overload magic and register QAbstractSocket::SocketError as metatype.
This is also descriped in the Qt docs:
Qt 5: http://doc.qt.io/qt-5/qabstractsocket.html#error
Qt 4.8 http://doc.qt.io/archives/qt-4.8/qabstractsocket.html#error
You may also use the SIGNAL(...) macro instead of the function pointer syntax to overcome this problem:
Example:
QObject::connect(clientConnection->tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayError(QAbstractSocket::SocketError)));
Upvotes: 1
Reputation: 4582
You need to overload the QAbstractSocket error:
connect(tcpSocket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error),
this, &ClientInterface::displayError);
Upvotes: 3