QNetworkAccessManager::finished(QNetworkReply * reply) signal doesn't work right.



  • Hi all, can someone help me with my little problem?

    I have this small function which I expected will check if I am connected to the Internet and if so it will send get request to Rurl
    and after downloading as hit finished(QNetworkReply*) signal it will go to finishedSlot(QNetworkReply*)

    Code:

    @void downloadrequest::sendRequest(QUrl Rurl){

    QNetworkConfigurationManager *mgr = new QNetworkConfigurationManager();
    if(!mgr->isOnline()){QMessageBox::warning(0, QString("Warning"), QString("you are not connected to the Internet"));}
    else {QNetworkAccessManager *nam = new QNetworkAccessManager(this);
    QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),
             this, SLOT(finishedSlot(QNetworkReply*)));
    QNetworkReply* reply = nam->get(QNetworkRequest(Rurl));}
    

    }@

    my problem is it never hit finished(QNetworkReply*) and I got stuck there and will never get to finishedSlot, I am pretty sure I am connected to the Internet and website I tested it with (www.google.com) is online and responding

    I am using Qt creator 2.4.1 with Qt 4.8.0.

    Thanks for help.



  • Perhaps you should also connect to the error signal of the returned reply.

    By the way: who is responsible for destructing the QNAM in your code? The idea is that you use a single QNAM in your application, instead of creating a new QNAM for every request.



  • Forgot about destructing it thanks for reminding me about it :)
    and I have already connected error signal in finishedSlot. I have also put it into sendRequest function but no error was reported, I am confused about this because it's really basic function and i can't find out why it doesn't work...



  • Further to Andre's comments (with which I concur), the following example works just fine for me:

    widget.h
    @
    #ifndef WIDGET_H
    #define WIDGET_H

    #include <QtGui>
    #include <QtNetwork>
    #include <QDebug>

    class Widget : public QWidget{
    Q_OBJECT
    public:
    Widget(QWidget parent=0) : QWidget(parent)
    {
    mgr=new QNetworkConfigurationManager;
    nam=new QNetworkAccessManager(this);
    connect(nam, SIGNAL(finished(QNetworkReply
    )),
    this, SLOT(replyFinished(QNetworkReply*)));

        QVBoxLayout *l=new QVBoxLayout(this);
        QPushButton *b=new QPushButton("Send", this);
        connect(b, SIGNAL(clicked()), this, SLOT(send()));
        l->addWidget(b);
    }
    

    private:
    QNetworkConfigurationManager *mgr;
    QNetworkAccessManager *nam;

    public slots:
    void send(){
    if(!mgr->isOnline()){
    qDebug() << "Not connected to the internet...";
    return;
    }
    nam->get(QNetworkRequest(QUrl("http://www.google.co.uk/")));
    }

    void replyFinished(QNetworkReply* reply){
        qDebug() << "Reply:\n" << reply->readAll();
        reply->deleteLater();
    }
    

    };

    #endif // WIDGET_H
    @

    main.cpp
    @
    #include <QtGui>
    #include <widget.h>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec&#40;&#41;;
    

    }
    @

    Hope this helps ;o)



  • I used a network monitor to see what happens on my ethernet port after using this function and I'm even more confused. It sent DNS request and got reply from DNS server, but as it sent http get request to google server (I also tried other servers) no reply came back and I have no idea why.



  • .



  • you could connect even QNetworkReply* reply finished() or downloadProgress() to slots ... finished() sent by QNetworkAccessManager is in tandem(one after another ) with that sent by its QNetworkReply


Log in to reply
 

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