0

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);
Jacob Krieg
  • 2,834
  • 15
  • 68
  • 140

1 Answers1

6

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

Dmitry Sazonov
  • 8,801
  • 1
  • 35
  • 61
gomons
  • 1,946
  • 14
  • 25
  • 1
    I must say I didn't know about this...seeing this horror syntax for overloaded slots makes me ask myself if it wouldn't be better to stick with the old syntax... – Jacob Krieg Apr 17 '15 at 22:05
  • 4
    New signal/slot syntax is type safe. With old syntax you can compile program with type errors. – gomons Apr 17 '15 at 22:07
  • 2
    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 – Dmitry Sazonov Apr 17 '15 at 22:22
  • 1
    @all I understand the reasons, pros and cons; just saying that the syntax is horror, that's just all... – Jacob Krieg Apr 18 '15 at 16:50