Jacob Krieg
Jacob Krieg

Reputation: 3184

New QObject::connect syntax doesn't work

I'm using the new syntax for QObject::connect to connect a signal to a slot:

connect(m_pNetworkReply, &QNetworkReply::error, this, &MyClass::onError);

but I'm getting this weird error and I have no idea why:

/home/user/Programming/sourcefile.cpp:131: error: no matching function for call to 'MyClass::connect(QNetworkReply*&, <unresolved overloaded function type>, MyClass* const, void (MyClass::*)(QNetworkReply::NetworkError))'
          this, &MyClass::onError);
                                        ^

The strange thing is if I use the old syntax, everything works fine:

connect(m_pNetworkReply, SIGNAL(QNetworkReply::error(QNetworkReply::NetworkError)),
        this, SLOT(MyClass::onError(QNetworkReply::NetworkError)));

Also, this connect works fine:

connect(m_pNetworkReply, &QNetworkReply::finished,
        this, &MyClass::replyFinished);

Also void onError(QNetworkReply::NetworkError); is a private slot.

What am I doing wrong?

EDIT:

Please note that this is not code that should work. I wrote this class just to isolate the problem; the code should compile though.

myclass.h:

#ifndef MYCLASS_H
#define MYCLASS_H

#include <QWidget>
#include <QNetworkAccessManager>
#include <QNetworkReply>

class MyClass : public QWidget
{
    Q_OBJECT

public:
    explicit MyClass(QWidget *parent = 0);
    ~MyClass();

private slots:
    void replyFinished();
    void onError(QNetworkReply::NetworkError);
    void onSslErrors(QList<QSslError>);

private:
    QNetworkAccessManager m_networkAccessManager;
    QNetworkReply *m_pNetworkReply;

};

#endif // MYCLASS_H

myclass.cpp:

#include "myclass.h"

#include <QNetworkRequest>


MyClass::MyClass(QWidget *parent) : QWidget(parent)
{
    QNetworkRequest networkRequest;

    connect(m_pNetworkReply, &QNetworkReply::finished,
            this, &MyClass::replyFinished);
    connect(m_pNetworkReply, &QNetworkReply::error,
            this, &MyClass::onError);
    connect(m_pNetworkReply, &QNetworkReply::sslErrors,
            this, &MyClass::onSslErrors);

}

MyClass::~MyClass()
{

}

void MyClass::replyFinished()
{
    disconnect(m_pNetworkReply, &QNetworkReply::finished,
               this, &MyClass::replyFinished);
//  disconnect(m_pNetworkReply, &QNetworkReply::error,
//             this, &MyClass::onError);
    disconnect(m_pNetworkReply, &QNetworkReply::sslErrors,
               this, &MyClass::onSslErrors);

    m_pNetworkReply->deleteLater();

    close();
}

void MyClass::onError(QNetworkReply::NetworkError)
{
    disconnect(m_pNetworkReply, &QNetworkReply::finished,
               this, &MyClass::replyFinished);
//  disconnect(m_pNetworkReply, &QNetworkReply::error,
//             this, &MyClass::onError);
    disconnect(m_pNetworkReply, &QNetworkReply::sslErrors,
               this, &MyClass::onSslErrors);
}

void MyClass::onSslErrors(QList<QSslError>)
{
    disconnect(m_pNetworkReply, &QNetworkReply::finished,
               this, &MyClass::replyFinished);
//  disconnect(m_pNetworkReply, &QNetworkReply::error,
//             this, &MyClass::onError);
    disconnect(m_pNetworkReply, &QNetworkReply::sslErrors,
               this, &MyClass::onSslErrors);
}

The only error I got is here:

connect(m_pNetworkReply, &QNetworkReply::error,
            this, &MyClass::onError);

Upvotes: 0

Views: 885

Answers (1)

gomons
gomons

Reputation: 1976

Change connect call to this:

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

Added info:

C++ doesn't know, what slot to choose if them are overrided (same name with different arguments). So, pointer to a slot provide not enough information to resolve connection. Static cast tells to the compiler exact slot type. Same with old syntax - you need to pass arguments

Upvotes: 6

Related Questions