I had a look at this link and except the signal spy and debugging i tried at least everything. I took the example from this site. 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?
Do I have a problem with my finished signal?
From the flow of the program i assume that it is the following:
I create a new pointer to an instance of class QNetworkAccessManager
named manager.
Then I connect the manager object's signal finished
to the custom slot test(QNetworkReply*)
from the object manager.
Now everytime the signal finished
from object manager is emitted, the function test
from object manger should be executed.
From the Documentation QNetworReply *QNetworkAccessManager::get(QNetworkRequest(QUrl))
I assume that when this function is called a new QNetworkReply*
object is returned and when this object is finished processing the finished
signal is emited. Do i have to connect my signal and slot in another way?
I tried to declare QNetworkReply reply* = manager->get....
and connect(reply, SIGNAL(finished()),this,SLOT(test()));
but here my application crashes.
When i set up wireshark and filter: http contains "http://www.theuselessweb.com" nothing pops up.
switchwebpanel.h
#ifndef SWITCHWEBPANEL_H
#define SWITCHWEBPANEL_H
#include <QNetworkAccessManager>
#include <QObject>
#include <QUrl>
#include <QNetworkReply>
#include <QFile>
#include <QDateTime>
#include <QNetworkRequest>
#include <QDebug>
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
#include <switchwebpanel.h>
switch_panel::switch_panel(QObject *parent):
QObject(parent)
{}
void switch_panel::doDownload(){
qDebug()<<"Downloader gestartet";
manager = new QNetworkAccessManager (this);
qDebug()<<"connect...";
// Here i want to call the test(QNetworkReply*) function
connect(manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(test(QNetworkReply*)));
qDebug()<<"get";
manager->get(QNetworkRequest(QUrl("http://www.theuselessweb.com")));
qDebug()<<"manager gegeted";
}
void switch_panel::replyFinished(QNetworkReply *reply){
qDebug()<<"Async starten";
if(reply->error()){
qDebug()<<"error";
qDebug()<<reply->errorString();
}
else {
qDebug()<<reply->header(QNetworkRequest::ContentTypeHeader).toString();
qDebug()<<reply->header(QNetworkRequest::LastModifiedHeader).toDateTime().toString();
qDebug()<<reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
qDebug()<<"File";
QFile *file = new QFile("./htmlpanel.txt");
if(file->open(QFile::Append)){
file->write(reply->readAll());
file->flush();
file->close();
}
//delete file;
}
reply->deleteLater();
qDebug()<<"Async fertig";
}
void switch_panel::test(QNetworkReply *reply){
qDebug()<<"test";
}
I get the following output:
Download
Downloader gestartet
connect...
get
manager gegeted
My Main.cpp calls: switch_panel panel; panel.doDownload();
Qt's network manager requires an event loop to run, to handle the asynchronous network access. The easiest way is to create a QCoreApplication
object in your main and call its exec()
method:
#include <QCoreApplication>
#include "switchwebpanel.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
switch_panel p;
p.doDownload();
return a.exec();
}