mercredi 28 septembre 2016

QT Connect QNetworkReply not firing

I know there are a lot of threads already, but... yeah.
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 changes 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 "ht tp://www.theuselessweb.com" nothing pops up. (No more then 2 links for reputation 10 or less)

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://ift.tt/YNxqAc")));
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

Aucun commentaire:

Enregistrer un commentaire