Change text displayed by QProgressBar
Asked Answered
O

4

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?

Odessaodetta answered 15/12, 2011 at 2:55 Comment(0)
V
34

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)+"%");
}
Villalpando answered 15/12, 2011 at 3:49 Comment(8)
I think i explained myself wrong (If so, sorry.) i want to add more text to the bar. Since it only shows the percentage.Odessaodetta
On Mac-OSX text on a QProgressbar seems impossible.Kapp
You will need to change style to CDE, CleanLooks, Motif, or Plastique. Documentation says QMacStyle never draws the text.Villalpando
Thanks for note about QMacStyle. Avoid me banging my head trying to display the label ;)Talebearer
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?Trellas
@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.Overstreet
@Trellas 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 for relevant discussion.Exciter
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.Anomalistic
H
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.

Harmonist answered 15/12, 2011 at 5:42 Comment(0)
R
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")
Rigel answered 16/6, 2020 at 16:36 Comment(0)
V
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);
    }
}
Verbose answered 2/3, 2015 at 18:13 Comment(1)
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!Dreyer

© 2022 - 2024 — McMap. All rights reserved.