Search code examples
qtwindowsqprogressbar

Qt Progressbar percentage issue


I want to display progressbar percentage when downloading a file. When file is downloaded i get 64%, not 100%. How to fix this issue? Thanks in advance.enter image description here

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"));
}

Solution

  • Your progressPercentage calculation is wrong. At 100%, you did 80*80/100 = 64.

    Change it to

    int progressPercentage = (totalMBReceived * 100) / totalSize;