Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Cant get QNetworkReply error continuosly...



  • Hello to all,

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

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

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

    Note: - I'm using the timer for getting the information continuously.
    - I'm using QT 5.13.1 and I have added QT += network on .pro file


  • Moderators

    @bladekel
    i think the issue is that you simply overwrite the same reply pointer each time the timer fires, but the signal-connections are still available.
    So if your requests needs longer than 2s (which is very likely when u expect a timeout error) it rather deletes the last created reply, instead the reply object actually created the error.

    You shouldn't start the next request not before the last one has triggered it's finished signal at least.



  • @raven-worx Actually my timer loop will be 24hours. So you suggest me to create signal everytime I call request but wont it be problem after a while because there will be lots of signals ???


  • Moderators

    @bladekel

    timer->start(2000);

    2000ms = 2s


Log in to reply