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

How to recognize QNetWorkReply.Error() form QNetWorkManager.get() or QNetWorkManager.put()



  • B.cpp
    QUrl url;
    QNetworkAccessManager manager;
    
    QNetworkReply *pReply = manager.put(QNetworkRequest(url), data);
    connect(pReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SIGNAL(error(QNetworkReply::NetworkError)));
    
    QNetworkReply *pReply = manager.get(QNetworkRequest(url));
    connect(pReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SIGNAL(error(QNetworkReply::NetworkError)));
    
    A.cpp
    void MainWindow::error(QNetworkReply::NetworkError error)
    {
        qDebug() << error;
        QMetaEnum metaEnum = QMetaEnum::fromType<QNetworkReply::NetworkError>();
        QNetworkReply *NReply = qobject_cast<QNetworkReply*>(sender());
        if(NReply->operation() == QNetworkAccessManager::PutOperation){// Crash NReply 0x0
            ui->label_error_upload->setText("Error: Upload " + QString(metaEnum.valueToKey(error)));
        }else{
            ui->label_error_download->setText("Error: Download " + QString(metaEnum.valueToKey(error)));
        }
    }
    


  • @sonichy said in How to recognize QNetWorkReply.Error() form QNetWorkManager.get() or QNetWorkManager.put():

    QNetworkReply *pReply = manager.put(QNetworkRequest(url), data); connect(pReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SIGNAL(error(QNetworkReply::NetworkError)));
     
    QNetworkReply *pReply = manager.get(QNetworkRequest(url));
    connect(pReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SIGNAL(error(QNetworkReply::NetworkError)));
    

    It seems strange, using the same pointer to hold two different objects...

    Remember that you're creating two replies, one per request; and they'll be handled in the same slot which will be invoked twice, and to know which reply object there will be straightforward



  • @sonichy said in How to recognize QNetWorkReply.Error() form QNetWorkManager.get() or QNetWorkManager.put():

    QNetworkAccessManager manager;

    Just for clarify, is your instance of QNetworkAccessManager still alive at the time your slots are called ? Because as you are declaring it on the stack, it may be already destroyed and I presume it will also destroy internal QNetworkReply objects.


Log in to reply