Qt QNetworkAccessManager长延时发出完成信号 [英] Qt QNetworkAccessManager long delay to emit finished signal

查看:1620
本文介绍了Qt QNetworkAccessManager长延时发出完成信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用QNAM来处理使用ftp协议的上传。
整个过程工作,但我有一个奇怪的行为:

I use a QNAM to handle uploads using a ftp protocol. The whole process works but I have a strange behavior:

这是我的方法:

void ftp::uploadFile(const QString &origin, const QString &destination)
{
    QUrl url("ftp://"+host+""+destination);
    url.setUserName(user);
    url.setPassword(pwd);
    url.setPort(21);

    localFile = new QFile(origin, this);
    if (localFile->open(QIODevice::ReadOnly))
    {
         reply = nam->put(QNetworkRequest(url), localFile);
         QObject::connect(reply, SIGNAL(uploadProgress(qint64, qint64)), SLOT(transferProgress(qint64, qint64)));        
         QObject::connect(reply, SIGNAL(finished()), this, SLOT(transferFinished()));
    }
    else qDebug() << localFile->errorString();
}

当我上传文件时,发出uploadProgress:

When I upload a file, the uploadProgress is emitted :

qDebug() << sent << "/" << total;

输出0 / x直到x / x。
然后,在发出完成的信号之前需要很长时间,可能长达20秒
为什么会出现这种延迟?

outputs the 0/x till the x/x . Then it takes a long time, maybe up to 20 seconds before the finished signal is emitted. Why this delay?

当进度小于$ c $时,我尝试忽略已完成的信号并发出信号 c> sent == total ,但文件已损坏在另一端。 (它不是真的损坏,因为我只发送jpg,结果文件是上半部分只有jpg。大部分只是灰色。)

I tried ignoring the finished signal and emit the signal myself when the progress is at sent==total but the file is corrupted at the other end. (It's not really corrupted, as I only send jpg, The resulting file is an upper-half only jpg. a big part is just grey.)

为我的用户提供一个进度条,其中100%真的意味着过程完成。
上传5秒,然后在100%保持20秒不是很好。

I'd like to provide my users with a progress bar where 100% really means the process is finished. Uploading for 5 seconds, then staying for 20 seconds at 100% isn't really nice.

推荐答案

文件上传在后台进行一些缓冲(qt套接字缓冲区,系统套接字缓冲区,网络缓冲区)所以'progress'信号只是意味着你发送数据到某个地方,也没有服务器收到它。
当所有传输到远程端和缓冲区的数据都被刷新时,发出完成信号。如果您需要知道传输的确切大小,您可以查找禁用请求或套接字或qnam buffring / caching。

file upload does some buffering in background (qt socket buffers, system socket buffers, network buffers) so 'progress' signal just means you send the data to somewhere nor that server has received it. While 'finished' signal is emitted when all data transferred to remote side and buffers are flushed. If you need to know exact size transferred you may look for disabling request or socket(s) or qnam buffring/caching.

这篇关于Qt QNetworkAccessManager长延时发出完成信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆