c++qtqnetworkaccessmanagerqnetworkreply

QNetworkReply error signal cant emits continuously


I need to get xml information from a web page and I can.

I do it with a timer for getting information continuously.

My problem is, I couldnt catch the error signals continuosly. I only catch it once.

I tried examples on QT help page, I read a lot but there is no succes.

Here are my codes;

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QDebug>
#include <QTimer>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QNetworkReply *reply;
    QNetworkRequest request;
    QNetworkAccessManager *manager;
    QTimer *timer;

private slots:
    void replyFinished(QNetworkReply*);
    void updateDB();
    void slotError(QNetworkReply::NetworkError);
    void slotSslErrors(QList<QSslError>);
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    manager = new QNetworkAccessManager(this);

    request.setUrl(QUrl("https://www.w3schools.com/xml/note.xml"));

    // Get response
    updateDB();

    // Signals for xml
    connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
    connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),this, SLOT(slotError(QNetworkReply::NetworkError)));
    connect(reply, SIGNAL(sslErrors(QList<QSslError>)),this, SLOT(slotSslErrors(QList<QSslError>)));

    // Update timer
    timer = new QTimer(this);
    timer->start(2000);
    connect(timer,SIGNAL(timeout()),this,SLOT(updateDB()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::replyFinished(QNetworkReply *)
{
    qDebug() << reply->readAll();
    reply->deleteLater();
}

void MainWindow::updateDB()
{
    reply = manager->get(request);
}

void MainWindow::slotError(QNetworkReply::NetworkError)
{
    qDebug() << "slotError";
    reply->deleteLater();
}

void MainWindow::slotSslErrors(QList<QSslError>)
{
    qDebug() << "slotSslErrors";
    reply->deleteLater();
}

Edit: I'm using QT 5.13.1 and I have added QT += network on .pro file


Solution

  • In your UpdateDB, you create a new reply:

    reply = manager->get(request);
    

    But you never connect its signals.

    The very first one gets its signals connected in the constructor, which is why it works for the first one.

    The fix is easy: move your signal connection for the reply from the constructor to the UpdateDB function, so this is done for every new reply object you create:

    reply = manager->get(request);
    connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),this, SLOT(slotError(QNetworkReply::NetworkError)));
    connect(reply, SIGNAL(sslErrors(QList<QSslError>)),this, SLOT(slotSslErrors(QList<QSslError>)));