Question

Well i have a QProgressBar where i show the download progress, however i want to set where it shows the percentage the download speed, leaving it as:

Percentage% (downloadspeed KB/s)

Any idea?

Was it helpful?

Solution

make the QProgressBar text visible.

QProgressBar *progBar = new QProgressBar();
progBar->setTextVisible(true);

to show the download progress

void Widget::setProgress(int downloadedSize, int totalSize)
{
    double downloaded_Size = (double)downloadedSize;
    double total_Size = (double)totalSize;
    double progress = (downloaded_Size/total_Size) * 100;
    progBar->setValue(progress);

    // ******************************************************************
    progBar->setFormat("Your text here. "+QString::number(progress)+"%");
}

OTHER TIPS

You could calculate the download speed yourself, then construct a string thus:

QString text = QString( "%p% (%1 KB/s)" ).arg( speedInKbps );
progressBar->setFormat( text );

You'll need to do this every time your download speed needs updating, however.

I know this is super late, but in case someone comes along later. Since PyQT4.2, you can just setFormat. For example to have it say currentValue of maxValue (0 of 4). All you need is

yourprogressbar.setFormat("%v of %m")

Because QProgressBar for Macintosh StyleSheet does not support the format property, then cross-platform support to make, you can add a second layer with QLabel.

    // init progress text label
    if (progressBar->isTextVisible())
    {
        progressBar->setTextVisible(false); // prevent dublicate

        QHBoxLayout *layout = new QHBoxLayout(progressBar);
        QLabel *overlay = new QLabel();
        overlay->setAlignment(Qt::AlignCenter);
        overlay->setText("");
        layout->addWidget(overlay);
        layout->setContentsMargins(0,0,0,0);

        connect(progressBar, SIGNAL(valueChanged(int)), this, SLOT(progressLabelUpdate()));
    }

void MainWindow::progressLabelUpdate()
{
    if (QProgressBar* progressBar = qobject_cast<QProgressBar*>(sender()))
    {
        QString text = progressBar->format();
        int precent = 0;
        if (progressBar->maximum()>0)
            precent = 100 * progressBar->value() / progressBar->maximum();
        text.replace("%p",  QString::number(precent));
        text.replace("%v", QString::number(progressBar->value()));
        QLabel *label = progressBar->findChild<QLabel *>();
        if (label)
            label->setText(text);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top