9
votes

I am using the new connect syntax for Qt5. QNetworkReply has a signal called error and also a function called error. This causes problems when attempting to connect to the signal:

connect(reply, &QNetworkReply::error, this, &MyClass::error);

error C2664: 'QMetaObject::Connection QObject::connect(const QObject *,const char *,const QObject *,const char *,Qt::ConnectionType)' : cannot convert parameter 2 from 'overloaded-function' to 'const char *' Context does not allow for disambiguation of overloaded function

How do I tell the compiler (MSVC) that I want to connect to the signal rather than the function?

2

2 Answers

17
votes

You need manually to cast the function pointer to address the correct function:

connect(reply, static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error), this, &MyClass::error);

This is the noted consequence of using new syntax for overloaded functions:

cons:

Very complicated syntax in cases of overloads?

P.S. You can find similar example on the above link:

 QObject::connect(socket, static_cast<void (QTcpSocket::*)(QAbstractSocket::SocketError)>(&QAbstractSocket::error), [socket] (QAbstractSocket::SocketError) {
        qDebug()<< "ERROR " << socket->errorString();
        socket->deleteLater();
    });
0
votes

Start from Qt 5.15 QNetworkReply::error is not a signal any more. You can connect to &QNetworkReply::errorOccurred instead, fortunately, without type casting:

connect(reply, &QNetworkReply::errorOccurred, this,
            [reply](QNetworkReply::NetworkError) {
               qCDebug() << "Error " << reply->errorString(); 
            });