如何很好地将Qint64"转换"为QProgressBar的int

the*_*inX 4 c++ qt qt4 integer-overflow

我正在玩QFtp(是的......我知道)并且一切正常.

使用他们自己的示例中的代码作为指导.

http://doc.qt.io/archives/qt-4.7/network-qftp-ftpwindow-cpp.html

我遇到的唯一问题是发送(或接收)大文件(比方说3 GB)时,进度条会出现故障.

这是由于从qint64到int的强制转换:

void FtpWindow::updateDataTransferProgress(qint64 readBytes, 
    qint64 totalBytes) 
{
    progressDialog->setMaximum(totalBytes);
    progressDialog->setValue(readBytes);
}
Run Code Online (Sandbox Code Playgroud)

我想知道在谷歌搜索大约一个小时之后处理这个问题最好的办法是什么,并通过确保我不会超出范围来确保它"安全".

while (totalBytes > 4294967295UL)
{ 
   totalBytes = totalBytes/4294967295UL;
   readBytes = readBytes/4294967295UL;
}
Run Code Online (Sandbox Code Playgroud)

但这并不"感觉"正确..

tro*_*foe 7

您可以使进度条以百分比形式显示进度:

void FtpWindow::updateDataTransferProgress(qint64 readBytes, 
    qint64 totalBytes) 
{
    progressDialog->setMaximum(100);
    progressDialog->setValue((qint)((readBytes * 100) / totalBytes));
}
Run Code Online (Sandbox Code Playgroud)

  • 当然,任何其他数字都不是100.但是如果你的progrssbar是700像素宽,使用百分比意味着它一次只能更新7个像素.就个人而言,我会选择4096. (4认同)
  • 刚上传一个0字节的文件时出现了0错误,所以我不得不添加:if(totalBytes!= 0) (2认同)