新的QObject :: connect语法不起作用

时间:2015-04-17 21:33:44

标签: qt qobject moc qmetaobject

我使用QObject::connect的新语法将信号连接到插槽:

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

但是我得到了这个奇怪的错误,我不明白为什么:

/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);
                                        ^

奇怪的是,如果我使用旧语法,一切正常:

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

此外,此连接工作正常:

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

同样void onError(QNetworkReply::NetworkError);private slot

我做错了什么?

修改

请注意,这不是应该有效的代码。我写这个课只是为了隔离问题;代码应该编译。

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);
}

我得到的唯一错误是:

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

1 个答案:

答案 0 :(得分:6)

将连接呼叫更改为:

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

添加信息:

C ++不知道,如果它们被覆盖,则选择哪个槽(具有不同参数的相同名称)。因此,指向插槽的指针提供的信息不足以解析连接。静态转换告诉编译器确切的槽类型。与旧语法相同 - 您需要传递参数