17

I use a QProgressBar to show the progress of a download operation. I would like to add some text to the percentage displayed, something like:

10% (download speed kB/s)

Any idea?

mins
  • 6,478
  • 12
  • 56
  • 75
Kazuma
  • 1,371
  • 6
  • 19
  • 33

4 Answers4

33

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)+"%");
}
Lwin Htoo Ko
  • 2,326
  • 4
  • 26
  • 38
  • I think i explained myself wrong (If so, sorry.) i want to add more text to the bar. Since it only shows the percentage. – Kazuma Dec 15 '11 at 04:27
  • On Mac-OSX text on a QProgressbar seems impossible. – TimZaman Jun 05 '14 at 08:09
  • You will need to change style to CDE, CleanLooks, Motif, or Plastique. Documentation says `QMacStyle never draws the text`. – Lwin Htoo Ko Jun 05 '14 at 08:56
  • 1
    Thanks for note about `QMacStyle`. Avoid me banging my head trying to display the label ;) – swdev Oct 10 '14 at 10:19
  • I tried this but does't work in my case. I have marquee progress bar setRange(0,0) and I am showing it in status bar of mainwindow. Is that because mine is in status bar? – zar Aug 03 '15 at 19:50
  • @zadane About the QMacStyle and the change to CDE etc. - is this even possible? I've read that for example you cannot use QMacStyle on other then a Mac platform and as far as I know CDE, Cleanlooks etc. are Linux themes so I'm not sure at all if that's even possible. – rbaleksandar Jan 15 '16 at 22:24
  • @zar No, that is because you confined the range to [0, 0]. A progress bar's format is *only* intended to interpolate the integer value of current progress into human-readable text. Since this value is always 0 for a progress bar confined to to [0, 0], Qt implicitly hides this text rather than displaying text resembling "0% complete." (Arguably, this is a bad thing.) One common workaround is to increase the range to [0, 1], which comes with its own undesirable tradeoffs. See [this StackOverflow answer](https://stackoverflow.com/a/31705094/2809027) for relevant discussion. – Cecil Curry Feb 23 '18 at 07:01
  • 1
    Actually this: `progBar->setFormat("Your text here. "+QString::number(progress)+"%");` can be raplaced with `progBar->setFormat("Your text here. %p%");` because **setFormat()** accepts some kind of formatting where **%p** is current percent, **%v** current value and **%m** - current number of steps. – Ihor Baklykov Aug 28 '20 at 12:13
9

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.

JediLlama
  • 1,153
  • 8
  • 8
4

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")
Josh Pachner
  • 151
  • 6
3

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);
    }
}
lexa-b
  • 1,759
  • 15
  • 15
  • Can you explain the code a little bit more? I am attempting to implement this in Python and my C++ is a bit rusty! Thanks! – Tom Myddeltyn May 11 '16 at 13:52