emit发送太快 qt,Qt QNetworkAccessManager长延时发出完成信号

I use a QNAM to handle uploads using a ftp protocol.

The whole process works but I have a strange behavior:

this is my method :

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();

}

When I upload a file, the uploadProgress is emitted :

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

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?

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.)

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.

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值