简体   繁体   English

发送信号时未调用Qt C ++插槽

[英]Qt C++ Slot is not called when Signal is sent

After debugging, I'm sure that the replyFinish() slot is not called when I call this->getNetReply(). 调试后,我确定在调用this-> getNetReply()时,不会确保没有调用replyFinish()插槽。 These are my files, in the main() fumction I call the getNetReply this way: Networking a; 这些是我的文件,在main()函数中,我这样称呼getNetReply: a.getNetReply(); a.getNetReply(); I did add QT+=network to my qmake. 我确实将QT + = network添加到我的qmake中。 Please help me. 请帮我。 Thank you very much. 非常感谢你。

my networking.cpp file 我的networking.cpp文件

#include "networking.h"
#include <QUrl>
#include <QNetworkRequest>

// constructor    

void Networking::getNetReply(){
    QNetworkAccessManager *man  = new QNetworkAccessManager(this);
    QObject::connect(man, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinished(QNetworkReply *)));
    qDebug() << "connected";
    QNetworkRequest req;
    req.setUrl(QUrl("http://www.google.com"));
    man->get(req);

}
// this method not called
void Networking::replyFinished(QNetworkReply *reply){
    QByteArray data = reply->readAll();
    QString str = QString(data);
    this->netRep = str;
    code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
}

my networking.h header file: 我的networking.h头文件:

#ifndef NETWORKING_H
#define NETWORKING_H

#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>

class Networking : public QObject
{
    Q_OBJECT
public:
    QString netRep;
    int code;
    explicit Networking(QObject *parent = 0);
    void getNetReply();

public slots:
    void replyFinished(QNetworkReply*);

};

#endif // NETWORKING_H

The get() function returns a QNetworkReply object. get()函数返回一个QNetworkReply对象。 Connect to the error signal of that object and it will tell you if an error happens (and which). 连接到该对象的错误信号,它将告诉您是否发生错误(以及发生了什么)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM