Qt Progressbar百分比问题
Qt Progressbar percentage issue
我想在下载文件时显示进度条百分比。下载文件时,我得到 64%,而不是 100%。如何解决这个问题?提前致谢。
void Updates::UpdateProgress(qint64 bytesRead, qint64 totalBytes)
{
int totalSize = totalBytes / 1024 / 1024;
int totalMBReceived = bytesRead / 1024 / 1024;
ui->progressBar->setMaximum(totalSize);
ui->progressBar->setValue(totalMBReceived);
int progressPercentage = (totalSize * totalMBReceived) / 100;
qDebug() << progressPercentage;
ui->label->setText(QString::number(progressPercentage).append("%"));
ui->label_4->setText(QString::number(totalSize).append(" MB") + " / " + QString::number(totalMBReceived).append(" MB"));
}
您的progressPercentage
计算有误。在 100% 时,您完成了 80*80/100 = 64.
改为
int progressPercentage = (totalMBReceived * 100) / totalSize;
我想在下载文件时显示进度条百分比。下载文件时,我得到 64%,而不是 100%。如何解决这个问题?提前致谢。
void Updates::UpdateProgress(qint64 bytesRead, qint64 totalBytes)
{
int totalSize = totalBytes / 1024 / 1024;
int totalMBReceived = bytesRead / 1024 / 1024;
ui->progressBar->setMaximum(totalSize);
ui->progressBar->setValue(totalMBReceived);
int progressPercentage = (totalSize * totalMBReceived) / 100;
qDebug() << progressPercentage;
ui->label->setText(QString::number(progressPercentage).append("%"));
ui->label_4->setText(QString::number(totalSize).append(" MB") + " / " + QString::number(totalMBReceived).append(" MB"));
}
您的progressPercentage
计算有误。在 100% 时,您完成了 80*80/100 = 64.
改为
int progressPercentage = (totalMBReceived * 100) / totalSize;