问题描述
您好
我想实现在一个单独的线程下载一个文件,并存储该文件,
,但我不能找到一个合适的方式来实现这没有邪恶的延迟(相当频繁的下载小文件,所以信号+插槽太慢)。
我想实现的:
(伪代码)
请求文件;
等待下载完成,超时或错误;
保存下载的文件;
如果可能,我更喜欢使用QNetworkAccessManager的示例。
感谢任何提示。
编辑:为了清楚,我想要的信号和插槽不是因为设计,因为缺乏速度。 >
Edit2:这个下载只是关于同步部分的下载文件,线程没有问题。
问题是,QT不提供一个api这样做,我不热衷于热情等待。
Edit3:示例代码,它应该工作,但不会:
QNetworkAccessManager net;
QNetworkReply * re(net.get(QNetworkRequest(QUrl(Qstring(www.blah.org/key)))));
if(re> waitForReadyRead(-1))//! @bug这不工作,假设,waitForRead返回false,并返回INSTANTLY!
qDebug()<< ReadyRead yeha !!!;
if(re> error()){
qDebug()< 无法下载<< re> url()。toString()
<< :<< re> errorString();
} else {
img-> load(re> readAll());
qDebug()<< Savin IMG;
}
delete re;
由于QHttp和QNetworkAccessManager都是异步的,你可以做的是使用单独的事件循环,基于QHttp的完整示例可以找到。它不应该太难以修改它为QNetworkAccessManager。
值得一提的是,你的印象是信号/插槽是慢可能是错误的。你实际上是否已经对你的代码进行了剖析来确定这一点?
无论你为信号/插槽支付的处罚是多少,看一下单个文件下载的时间。更重要的是,这是非常非Qt这样做。
在这一天结束如果,你确实患有信号/槽(这是再次,可疑),我建议不要使用Qt这个特定的任务,也许是老的C套接字是一个更好的主意(或围绕他们的薄包装,以节省错误处理,可能需要一些额外的工作)。
HiI want to achieve to download a file in a separate thread and store that file,but I was not able to find an appropriate way to achieve this without evil delay (quite frequent download of small files, so signal+slots are too slow).What I want to achieve:(Pseudo Code)
request file;
wait for download finishing, timeout or error;
save downloaded file;
I'd prefer an example with QNetworkAccessManager if possible.Thanks for any tipp.
Edit: Just to be clear, I want signal and slots not because of design aswell as the lack of speed.
Edit2: This download is only about the download file in sync part, threading is no problem.The problem is that QT does not provide an api for doing that and I am not keen on hotspinning wait.
Edit3: Example code like it should work, but does not:
QNetworkAccessManager net;
QNetworkReply *re (net.get( QNetworkRequest( QUrl( Qstring("www.blah.org/key") ) ) ));
if (re->waitForReadyRead(-1)) //! @bug this does not work as supposed, waitForRead returns false and returns INSTANTLY!!
qDebug() << "ReadyRead yeha!!!";
if (re->error()) {
qDebug() << "Can't download" << re->url().toString()
<< ":" << re->errorString();
} else {
img->load(re->readAll());
qDebug() << "Savin IMG";
}
delete re;
I needed something similar but for different reasons. Since QHttp and QNetworkAccessManager are both async what you could do is use a separate event loop, a full example based on QHttp can be found here. It shouldn't be too difficult to modify it for QNetworkAccessManager.
It's worth mentioning that your impression that signals/slots are "slow" is probably wrong. Have you actually profiled your code to determine this?
Whatever penalty you might be paying for signals/slots it's probably negligible when looking at the amount of time a single file download takes. More so, it's very "non Qt" to do things this way. These classes were designed like this for a reason.
At the end of the day if you are indeed suffering from signals/slots (which is again, doubtful), I would recommend not to use Qt for this particular task, maybe plain old C sockets are a better idea (or a thin wrapper around them to save the error handling which might require some extra work).
这篇关于C ++ / Qt下载文件同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!