Downloading a file with Qt?

一世执手 提交于 2019-12-20 05:13:16

问题


I am trying to figure out a way for my Qt browser app can download a word document from our web app. The web app is written in ExtJS, when (in a browser such as Chrome) a user clicks the "Download report" button a javascript event listener detects that click and opens a 0px0px iframe and the file downloads. I'm not sure how to replicate this browser feature in Qt?

When I click on the link I get a network reply of "Operation canceled" 5?

What class/method would be best to retrieve these files?


回答1:


to download a file you need : a QNetworkAccessManager in this case http. a QFile in this case file. a QNetworkReply in this case reply connect the reply with a slot that writes the bytes received through QNetworkAccessManager in this case the slot is called readingReadyBytes()

so i create the request and connect to my slot:

 const QNetworkRequest& request = QNetworkRequest(url);
 reply = http->get(request);
 QObject::connect(reply, SIGNAL(readyRead()), this,
        SLOT( readingReadyBytes() ));

then i create my slot:

 void yourClass::readingReadyBytes() {
    file->write(reply->read(reply->bytesAvailable()));
 }

finally you have to save and close your file. tipically is done when the QNetworkAccessManager emit the finished signal.. this is what i remember and i think it is all..



来源:https://stackoverflow.com/questions/13747548/downloading-a-file-with-qt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!