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

QNetworkManager : resend qnetworkrequest before end make it crash



  • Hey,

    I'm working on an embedded buying application. When the customer add a product to his cart, i send a QNetworkRequest with a webservice as URL to update the backoffice. But if the customer double click the add-to-cart button, i think that the request is resend before the end of the first and make the application exit with error : "QEventLoop::exec: instance 0x423190 has already called exec()".

    Here is my doRequest function :

    const QString&                            Request::doRequest(const QString& request, RequestType type, const QByteArray& data)
    {
        QObject::connect(&_networkManager, SIGNAL(finished(QNetworkReply*)), &_eventLoop, SLOT(quit()));
        QUrl url(request);
        QNetworkRequest req(url);
    
        switch (type)
        {
        case e_GET :
            _reply = _networkManager.get(req);
            break;
        case e_POST :
            _reply = _networkManager.post(req, data);
            break;
        case e_PUT :
            _reply = _networkManager.put(req, data);
            break;
        case e_DELETE :
            _reply = _networkManager.deleteResource(req);
            break;
        }
        _eventLoop.exec();
        if (_reply->error() == QNetworkReply::NoError)
            {
                QString result = _reply->readAll();
                delete _reply;
                return (result);
            }
        delete _reply;
        return ("");
    }
    

    Does anyone encounter the same problem ? Is there a work around?



  • One possible solution is to launch one thread for each request


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    That's because you are calling exec on _eventLoop twice. You should rather use a local QEventLoop or not allow the query to start if already in progress.