QT Connect QNetworkReply SIGNAL finished() not firing

admin

Administrator
Staff member
I had a look at this <a href="https://samdutton.wordpress.com/2008/10/03/debugging-signals-and-slots-in-qt/" rel="nofollow">link</a> and except the signal spy and debugging i tried at least everything. I took the example from <a href="http://www.bogotobogo.com/Qt/Qt5_Downloading_Files_QNetworkAccessManager_QNetworkRequest.php" rel="nofollow">this site</a>. I haven't set up the debugger for now.

My problem is that i want to fire a custom slot and nothing is happening.
I tried qmake and rebuild the project, but the result was the same.
I am using QTCreator with QT 5.7 msvc2015_64 as kit on a Windows10 machine.
I don't get any unreferenced warnings which should be for the test function (*reply not used). What could be the source of this problem?<br>
Do I have a problem with my finished signal?

From the flow of the program i assume that it is the following:<br>
I create a new pointer to an instance of class
Code:
QNetworkAccessManager
named manager.
Then I connect the manager object's signal
Code:
finished
to the custom slot
Code:
test(QNetworkReply*)
from the object manager.
Now everytime the signal
Code:
finished
from object manager is emitted, the function
Code:
test
from object manger should be executed.<br>
From the Documentation
Code:
QNetworReply *QNetworkAccessManager::get(QNetworkRequest(QUrl))
I assume that when this function is called a new
Code:
QNetworkReply*
object is returned and when this object is finished processing the
Code:
finished
signal is emited. Do i have to connect my signal and slot in another way?

I tried to declare
Code:
QNetworkReply reply* = manager-&gt;get....
and
Code:
connect(reply, SIGNAL(finished()),this,SLOT(test()));
but here my application crashes.

When i set up wireshark and filter: http contains "<a href="http://www.theuselessweb.com" rel="nofollow">http://www.theuselessweb.com</a>" nothing pops up.

switchwebpanel.h

Code:
#ifndef SWITCHWEBPANEL_H
#define SWITCHWEBPANEL_H
#include &lt;QNetworkAccessManager&gt;
#include &lt;QObject&gt;
#include &lt;QUrl&gt;
#include &lt;QNetworkReply&gt;
#include &lt;QFile&gt;
#include &lt;QDateTime&gt;
#include &lt;QNetworkRequest&gt;
#include &lt;QDebug&gt;

class switch_panel : public QObject
{
Q_OBJECT

public:
  switch_panel(QObject *parent = 0);
  void doDownload();

signals:

public slots:
  void replyFinished(QNetworkReply *reply);
  void test(QNetworkReply * reply);

private:
  QNetworkAccessManager *manager;

};

#endif // SWITCHWEBPANEL_H

switchwebpanel.cpp

Code:
#include &lt;switchwebpanel.h&gt;


switch_panel::switch_panel(QObject *parent):
QObject(parent)
{}


void switch_panel::doDownload(){
  qDebug()&lt;&lt;"Downloader gestartet";
  manager = new QNetworkAccessManager (this);
  qDebug()&lt;&lt;"connect...";

  // Here i want to call the test(QNetworkReply*) function

  connect(manager, SIGNAL(finished(QNetworkReply*)),
        this, SLOT(test(QNetworkReply*)));
  qDebug()&lt;&lt;"get";
  manager-&gt;get(QNetworkRequest(QUrl("http://www.theuselessweb.com")));
  qDebug()&lt;&lt;"manager gegeted";
}

void switch_panel::replyFinished(QNetworkReply *reply){
  qDebug()&lt;&lt;"Async starten";
  if(reply-&gt;error()){
    qDebug()&lt;&lt;"error";
    qDebug()&lt;&lt;reply-&gt;errorString();
  }
  else {
    qDebug()&lt;&lt;reply-&gt;header(QNetworkRequest::ContentTypeHeader).toString();
    qDebug()&lt;&lt;reply-&gt;header(QNetworkRequest::LastModifiedHeader).toDateTime().toString();
    qDebug()&lt;&lt;reply-&gt;attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
    qDebug()&lt;&lt;"File";
    QFile *file = new QFile("./htmlpanel.txt");
    if(file-&gt;open(QFile::Append)){
        file-&gt;write(reply-&gt;readAll());
        file-&gt;flush();
        file-&gt;close();
    }
    //delete file;
  }
  reply-&gt;deleteLater();
  qDebug()&lt;&lt;"Async fertig";
}
void switch_panel::test(QNetworkReply *reply){
  qDebug()&lt;&lt;"test";
}

I get the following output:

Code:
Download
Downloader gestartet
connect...
get
manager gegeted

My Main.cpp calls:
Code:
switch_panel panel; panel.doDownload();