QNetworkAccessManager signals not working



  • I don't know why but it doesn't work at all, not the finished, error, or sslErrors, none of them:

    auto manager = new QNetworkAccessManager;
    
    QNetworkRequest request;
    request.setUrl(QUrl("http://qt-project.org"));
    request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
    
    QNetworkReply *reply = manager->get(request);
    connect(reply, SIGNAL(readyRead()), this, SLOT(replyReadyRead()));
    connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
                    this, SLOT(replyError(QNetworkReply::NetworkError)));
    connect(reply, SIGNAL(sslErrors(QList<QSslError>)),
                    this, SLOT(replySslErrors(QList<QSslError>)));
    

    I even tried to use the new syntax like connect(networkReply, static_cast<void(QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error), [=](QNetworkReply::NetworkError code){ /* ... */ }); but doesn't work either...
    What is happening?


  • Moderators

    @Defohin

    • check the return value of connect()
    • check the console for warnings regarding the connect() statements
    • show some more code, what happen with the reply/QNetworkAccessManager?


  • @raven-worx

    1 - All of them is returning true
    2 - There's nothing on the console
    3 - I just realized that I forgot to connect finished ...

    auto manager = new QNetworkAccessManager;
    
    QNetworkRequest request;
    request.setUrl(QUrl("http://qt-project.org"));
    request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
    
    QNetworkReply *reply = manager->get(request);
    auto r1 = connect(reply, SIGNAL(finished()), this, SLOT(replyFinished()));
    auto r2 = connect(reply, SIGNAL(readyRead()), this, SLOT(replyReadyRead()));
    auto r3 = connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
            this, SLOT(replyError(QNetworkReply::NetworkError)));
    auto r4 = connect(reply, SIGNAL(sslErrors(QList<QSslError>)),
            this, SLOT(replySslErrors(QList<QSslError>)));
    
    qDebug() << r1 << r2 << r3 << r4;
    

    The rest of the code is the slots, but it has nothing special:

    void NYTimes::replyFinished()
    {
        qDebug() << "it is working";
    }
    
    void NYTimes::replyReadyRead()
    {
        qDebug() << "ready to read";
    }
    
    void NYTimes::replyError(QNetworkReply::NetworkError error)
    {
        qDebug() << error;
    }
    
    void NYTimes::replySslErrors(QList<QSslError> errors)
    {
        qDebug() << errors;
    }
    

    And yes, it's not working either.


  • Moderators

    @Defohin
    if all connects are successfull (by returning true), the only thing i can think of is that the reply (or QNAM) or the object holding the slots are deleted?



  • It's not being deleted, I'm using pointers to test if it has something to do with that, but it doesn't work either.



  • Hi,
    can you try with a site that does not redirect to https e.g. http://en.cppreference.com/w/. Maybe that's a problem.
    -Michael.



  • It doesn't work either, I really don't know what is going on.



  • This post is deleted!


  • Hi,
    please try to make QNetworkReply *reply a class member variable. As you have it now it gets lost as soon as you leave the function. You will need it anyway in the readyReadslot.
    -Michael.



  • The problem was on the instance of the class, I had to use it as a pointer.

    auto nytimes = new NYTimes;
    nytimes->parse();
    

    instead of:

    NYTimes nytimes;
    nytimes.parse();
    

    Is there a reason for that?


  • Qt Champions 2016

    Hi
    It depends on the context

    void Func() {
    auto nytimes = new NYTimes;
    nytimes->parse();
    } // after here NYTimes still lives

    void Func() {
    NYTimes nytimes;
    nytimes.parse();
    } // after here NYTimes is no more



  • I was calling NYTimes nytimes; on the constructor of the QMainWindow


  • Qt Champions 2016

    @Defohin said in QNetworkAccessManager signals not working:

    NYTimes nytimes; on the constructor of the QMainWindow

    So it means that at the time when
    MainWindow was shown, nytimes would have been deleted.
    Its called "running out of scope". Applies to all variables on the stack.



  • Thank you, it's solved


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.