QNetworkReply wait for finished

前端 未结 2 1868

I am using Qt 4.6.3 and the following not-working code

QStringList userInfo;
QNetworkRequest netRequest(QUrl(\"http://api.stackoverflow.com/1.1/users/587532\         


        
2条回答
  •  暖寄归人
    2020-12-15 23:32

    First I recommend you to read the relevant documentation from the Qt Documentation Reference that you can find here: http://doc.qt.nokia.com/latest/classes.html.

    Looking at your code sample it seems that you already have, along side with QNetworkRequest and QNetworkReply, a QNetworkAccessManager. What you need is to connect a slot to the finished(QNetworkReply *) signal. This signal is emitted whenever a pending network reply is finished.

    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply*)),
            this, SLOT(replyFinished(QNetworkReply*)));
    
    manager->get(QNetworkRequest(QUrl("http://api.stackoverflow.com")));
    

    Now, in your slot, you can read the data which was sent in response to your request. Something like:

    void MyClass::MySlot(QNetworkReply *data) {
        QFile file("dataFromRequest");
        if (!file.open(QIODevice::WriteOnly))
            return;
        file.write(data->readAll());
        file.close();
    }
    

    EDIT:

    To wait synchronously for a signal use QEventLoop. You have an example here

    http://wiki.forum.nokia.com/index.php/How_to_wait_synchronously_for_a_Signal_in_Qt

提交回复
热议问题