How to nicely “cast” qint64 to int for QProgressBar

自作多情 提交于 2019-12-23 12:23:27

问题


I'm playing around with QFtp (yes .. I know) and all works well.

Using code from their own example(s) as a guideline.

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

The only problem I'm having is when sending (or receiving) big files (let's say 3 GB) the progress bar glitches out.

This is due to the cast from qint64 to int in:

void FtpWindow::updateDataTransferProgress(qint64 readBytes, 
    qint64 totalBytes) 
{
    progressDialog->setMaximum(totalBytes);
    progressDialog->setValue(readBytes);
}

I'm wondering what would be the nicest way to handle this after googling for about an hour and settling on keeping it 'safe' by making sure I don't go out of range.

while (totalBytes > 4294967295UL)
{ 
   totalBytes = totalBytes/4294967295UL;
   readBytes = readBytes/4294967295UL;
}

But that doesn't "feel" right . .


回答1:


You can make the progress bar present the progress as a percentage:

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



回答2:


Set your progress bar to a range of 0-100, and display the percentage of bytes read instead of trying to set the absolute value.



来源:https://stackoverflow.com/questions/4931780/how-to-nicely-cast-qint64-to-int-for-qprogressbar

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