0

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

king_nak
  • 11,313
  • 33
  • 58
Eggi
  • 170
  • 1
  • 15

1 Answers1

0

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();
}
king_nak
  • 11,313
  • 33
  • 58
  • 2
    Ok i took your advice and implemented an event loop, but in another way: reply = manager->get(QNetworkRequest(QUrl("http://www.theuselessweb.com"))); qDebug()<<"connect"; QEventLoop loop; connect(reply,SIGNAL(finished()),&loop,SLOT(quit())); loop.exec(); – Eggi Sep 28 '16 at 09:45
  • Qt 5.5 issues a warning that `QEventLoop` cannot be used without a `QApplication` at runtime. It might work, but you should create an instance in main. Nevertheless, you can use a local event loop for your get request. – king_nak Sep 28 '16 at 09:56
  • Also note that the call to `loop.exec()` will never return! You might want to create a `QEventLoop *` in your `switch_panel` class and call `quit()` when you're finished with the request, to resume execution – king_nak Sep 28 '16 at 09:58
  • `loop.exec()` does return. When the `finished()` signal is emitted, the connect before the `loop.exec()` will cause the event loop to quit, making it return from exec – Kevin Krammer Sep 28 '16 at 13:25
  • @KevinKrammer You're right, I misread the connect... This is a much more elegant way – king_nak Sep 29 '16 at 08:41